MATLAB: How to convert all the ‘single’ type variables in the workspace to ‘double’

allconvertdoubleMATLABsingleworkspace

I need to convert all the 'single' variables in my workspace to 'double'. However, I do not want to use the function DOUBLE on every variable. I would like a small piece of code in my file, that can ensure that all the variables have been set to type 'double'.

Best Answer

The following example demonstrates a way to do this in MATLAB.
s = whos;
for i = 1:length(s)
if strcmp(s(i).class,'single')
name = s(i).name;
assignin('base', name, double(evalin('base', name)));
end
end
This code, upon execution, searches for any 'single' types in the current workspace, and converts them to 'double'.