How can I create a structure with a loop?

57 Ansichten (letzte 30 Tage)
Lucas Lesueur
Lucas Lesueur am 23 Jan. 2017
Kommentiert: Stephen23 am 23 Jan. 2017
Hi,
It has been hours that I'm struggling and still ...no way to solve it. I guess I just need a quick tip!
I want to create a new structure, based on some already existing structures called "Measurement1", "Measurement2" ects... (size : 1x1). But I would like to do it in a smart way by using a loop. It works when I assigned each of them directly, but as soon as it's about a loop, it generates the error "Index exceeds matrix dimensions."
You will find below my code where each value is assigned arbitrary, just in order to give you an idea of what I'm looking for. Instead of having "1","2", I would like to index it.
MeasurementNumber = 'MeasurementData';
Pressure = 'Pressure';
Frequency = 'Frequency';
Absorbance = 'Absorbance';
value = {Measurement1;
Measurement2;}
value2 = {Measurement1.PRESSURE;
Measurement2.PRESSURE;}
value3 = {Measurement1.FREQ;
Measurement2.FREQ;}
value4 = {Measurement1.ABSORBANCE;
Measurement2.ABSORBANCE;}
Structure = struct(MeasurementNumber,value,Pressure,value2,Frequency,value3,Absorbance,value4)
I got the error when I run this one :
for i=1:nFile;
value = {Measurement(i)};
end
Thank you for your precious help ! (Sorry for potential English mistakes, I'm not a native speaker !)
  1 Kommentar
Stephen23
Stephen23 am 23 Jan. 2017
Bearbeitet: Stephen23 am 23 Jan. 2017
"I want to create a new structure, based on some already existing structures called "Measurement1", "Measurement2" ects... (size : 1x1). But I would like to do it in a smart way by using a loop"
This is never going to be efficient or reliable. Although beginners love to access variable names dynamically, in reality doing this is slow and buggy:
"It has been hours that I'm struggling and still ...no way to solve it"
Trying to access variable names dynamically will be slow and buggy code. Rather than write buggy code, you could improve the code design.
The first question is: how did all of these numbered variables get into the workspace? If they were load-ed, then the best solution is to load them into a structure:
S = load(...)
and from there take the required fields and merge this into one variable (e.g. a cell array or ND numeric array). When you do this then the rest of your code will simpler, faster, and much more robust. You need to learn that the decision to puts lots of (numbered) variables into the workspace forces you to use buggy and slow code. When you learn to import your data into one variable then the rest of your code can be efficient and robust.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 23 Jan. 2017
Bearbeitet: Stephen23 am 23 Jan. 2017
Non-scalar Structure: I do not really follow your example, but perhaps the best solution wold be to use a non-scalar structure, which are very simple and allow you to use indexing in a loop:
S(1).data = 1:3;
S(1).name = 'Anna';
S(2).data = 4:6;
S(2).name = 'Bob';
S(3).data = 7:9;
S(3).name = 'Cat';
Acessing this non-scalar structure is very simple, you can loop over its elements:
for k = 1:numel(S)
S(k).name
end
which prints
ans =
Anna
ans =
Bob
ans =
Cat
or generate a comma separated list and concatenate these into arrays for doing your work:
>> mat = vertcat(S.data)
mat =
1 2 3
4 5 6
7 8 9
>> names = {S.name}
names =
'Anna' 'Bob' 'Cat'
Avoid Numbered Variables! Whatever you do, do not put lots of numbered variables into your workspace! Note that if each variable has a number then this is a de-facto index: so why not turn into into a real index (of one variable), which would allow you to use fast and efficient indexing?
Instead of this (slow and buggy to access)
var1 = ...
var2 = ...
var3 = ...
this is much better:
var(1) = ...
var(2) = ...
var(3) = ...
No workspace clutter, much faster, less buggy, easier to read, easier to bug fix, lets you use the inbuilt Editor mlint and code-helper tools...
  2 Kommentare
Lucas Lesueur
Lucas Lesueur am 23 Jan. 2017
Thank you for your quick answer. You are right Stephen, trying to access variable names dynamically seems to be really complex, it might be alright to do this but it probably requires an advanced skill level with Matlab.
Anyway I managed to solve the problem based on your suggestion to not numbered variable. Thank you :)
Stephen23
Stephen23 am 23 Jan. 2017
@Lucas Lesueur: I am glad that you found a good solution.
"it probably requires an advanced skill level with Matlab"
Actually advanced users do not do this, because it is very inefficient and buggy. They use indices, just like you.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 23 Jan. 2017
When creating the data you should assign to Measurement(i) where i is your file index, not to Structure
  1 Kommentar
Lucas Lesueur
Lucas Lesueur am 23 Jan. 2017
It is more complex, each Measurement files comes from a recording devices that automatically saved everything as a Matlab script. When you run this particular file, it automatically creates the structure corresponding to the measurement. Sorry I forgot this detail.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Variables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by