create string of characters from variable name matlab

3 Ansichten (letzte 30 Tage)
Ouerdia Tahar
Ouerdia Tahar am 25 Apr. 2018
Kommentiert: Ouerdia Tahar am 26 Apr. 2018
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 Kommentare
Stephen23
Stephen23 am 25 Apr. 2018
@Ouerdia Tahar: you did not ask us anything. What is your question?
Ouerdia Tahar
Ouerdia Tahar am 25 Apr. 2018
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Apr. 2018
Bearbeitet: Stephen23 am 25 Apr. 2018
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 Kommentare
Stephen23
Stephen23 am 25 Apr. 2018
Bearbeitet: Stephen23 am 26 Apr. 2018
"...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.
Ouerdia Tahar
Ouerdia Tahar am 26 Apr. 2018
Thank you so much, it was very helpful

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by