MATLAB: Create string of characters from variable name matlab

nan

I want to create string of characters from variable name And use it as a name of an excel file. It's easy to create a single string but I want to write a function that generalizes this situation. For my part: I have a signal that I have to cut into several windows, for each window I have to calculate values (5 values) And in the end I have to store: for each window of a signal, its parameters in a file
Example:
Signal S.
I cut signal S into 3 windows; I need to create 3 excel files:
S_1
S_2
S_3
Proposed solution :
function str = display (signal, i)
formatSpec = "% s_% d";
str = sprintf (formatSpec, signal, i)
end
function FeatureExtract (signal, numOfwindows)
for i = 1: numOfwindows
feature =parameters (signal);
str = display (signal, i)
end
end
Main program:
FeatureExtract (mysignal)
I want this result: 3 excel files for 3 windows:
Mysignal_1
Mysignal_2
Mysignal_3

Best Answer

You could use inputname:
function FeatureExtract(signal,numOfwindows)
name = inputname(1);
fmt = '%s_%f';
for k = 1:numOfwindows
...
str = sprintf(fmt,name,k);
...
end
end
Note that most likely this is a bad idea anyway, because it implies that you are storing meta-data in variable names. Read these to know why putting meta-data into variable names is a bad idea: