create string of characters from variable name matlab

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

2 Comments

@Ouerdia Tahar: you did not ask us anything. What is your question?
the problem when I write that what is displayed is not the name of my signal (which I want to use for my file ) but the values of my signal I want to collect the name of my signal to use it as my file name How to do it?

Sign in to comment.

 Accepted 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:

5 Comments

It works thank you , if you said that is a bad idea, How to do it proprely ?
"if you said that is a bad idea,"
It is not just me who says that, read the links I gave you.
"How to do it proprely ?"
I don't think there is a "proper" way to access a variable's names. The point is that meta-data should not be in the variable name in the first place, so there should be no reason to access a variable name anyway. For example, you could simply put your data in a variable named data, and the meta-data in a variable named name, then you can trivially access them without this awkward messing around with variable names:
data = [...]
name = 'SampleS';
...
save([name,'.mat'],'data')
I understand, I read the document the fact is that I have to change the name every time I have an other signal (I have multiple signal ) By doing what you're suggesting, I'll have a hundred lines of code by storing every time time the data, giving the name to each signal
function display(signal)
data = signal;
name = 'signal';
save([name,'.mat'],'data');
fmt = '%s_%f';
str = sprintf(fmt,signal)
end
so when I call my function I have always the data displayed not the name of my signal
Thank you for answering me
"...the fact is that I have to change the name every time I have an other signal..."
And that is the bad code design decision right there!
Changing the variable name for each measurement or signal is the reasons why you are forcing yourself into writing slow, complex code. I work with thousands of files, with millions of separate measurements, all imported automatically... but I never change the variable names for each file or measurement. By storing any meta-data as data in its own right, my code is simpler, neater, easier to debug, and much more efficient. You might like to read this to know why putting meta-data into variable names is a bad idea:
"By doing what you're suggesting, I'll have a hundred lines of code by storing every time time the data, giving the name to each signal"
Errr, not at all. In fact, what I suggested would mean the exact opposite: that your code would be simpler, neater, and more efficient. And it would not require the creation of hundreds of lines of code.
The function that you showed is totally unrelated to what I suggested. If you store the meta-data with the data (e.g. when it is read from file, or simulated, or whatever) then you can trivially avoid this slow, complex approach to writing code that you have now.
EXAMPLE Here is a simple example of what I suggest, using a non-scalar structure for clarity (but it could be any kind of array: numeric, cell, table, etc). First load/define your data and meta-data (e.g. signal name):
for k = ...
S(k).data = load /define data values.
S(k).name = define meta-data, e.g. signal name.
S(k).date = any other meta-data that you want.
end
and then to process the data all that is required is to loop over the elements of the structure, which is simple and very efficient. E.g. to call sprintf like in your question:
for k = 1:numel(S)
str = sprintf(fmt,S(k).name,k);
S(k).data % do something with the data
...
end
Thus I can easily write simple, efficient code that does not need to access variable names (which is always slow), uses fast and efficient indexing, and does not require writing hundreds of lines of code.
Thank you so much, it was very helpful

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!