How process many structured arrays at once
7 ビュー (過去 30 日間)
古いコメントを表示
I want to plot/process some test data, but it was stored in structured arrays with unhelpful / sequential names (Signal_000, Signal_001 ...Signal_157). I would like to sequentially extract the character array, make it a string, turn it into a variable name, and then assign the data in that structured array.
All the posts I've seen say don't do this (was considering eval). How should I go about doing this?
3 件のコメント
jonas
2018 年 8 月 30 日
編集済み: jonas
2018 年 8 月 30 日
And you received several methods to solve your problem. If you are unable to solve it by now, then you should consider uploading a sample data set and be thankful that people are willing to help.
I'm guessing most people here (at least myself) get some satisfaction from helping people improve their coding. Solving a trivial problem by writing buggy code is not something I will spend my evening doing.
採用された回答
Stephen23
2018 年 8 月 31 日
編集済み: Stephen23
2018 年 8 月 31 日
"I want to plot/process some test data, but it was stored in structured arrays with unhelpful / sequential names (Signal_000, Signal_001 ...Signal_157). I would like to sequentially extract the character array, make it a string, turn it into a variable name, and then assign the data in that structured array."
So far the actual data files have not been described very well: I am going to assume that names of the .mat files corresponds exactly to the names of the structures, that each file/structure has a different name, and that each file contains one structure only. This can be processed very easily, something like this:
D = 'path of the folder where the data files are';
S = dir(fullfile('*.mat'));
C = cell(1,numel(S));
for k = 1:numel(S)
T = load(fullfile(D,S(k).name));
C(k) = struct2cell(T);
C(k).filename = S(k).name;
end
You now have all of the data in one cell array C, which is trivial to process in a loop. For example, your example data for file 0045 would then be
Z{46}.y_values.quantity.g
Z{46}.function_record.name
and the filename is always available:
Z{46}.filename
If all of the structures contain the same fields, then you can easily convert this cell array into one non-scalar structure like this:
Z = [C{:}]
Your example data for file 0045 would then be
Z(46).y_values.quantity.g
Z(46).function_record.name
etc
and you can do neat things, like get all of filenames in one cell array:
{Z.filename}
7 件のコメント
Stephen23
2018 年 8 月 31 日
編集済み: Stephen23
2018 年 8 月 31 日
@RichardB: using struct2cell is a very good idea, together with fieldnames. Something like this works on your sample file (and a simple copy I made):
D = '.'; % directory where the files are:
S = dir(fullfile(D,'*.mat'));
C = cell(1,numel(S));
for k = 1:numel(S)
T = load(fullfile(D,S(k).name));
F = fieldnames(T);
T = struct2cell(T);
T = vertcat(T{:});
[T.fieldname] = F{:};
[T.filedata] = deal(S(k));
C{k} = T;
end
Z = vertcat(C{:});
It returns one structure Z which contains all of the file data, the filenames, and the fieldnames. So you can process these however you want. The output is easy to access, e.g. the second element of the structure contains the data from :
>> Z(2).filedata.name
ans =
SampleRun1.mat
>> Z(2).fieldname
ans =
Signal_005
>> Z(2).x_values
ans =
start_value: 7.66712283871882e-05
increment: 9.765625e-05
number_of_values: 4122210
quantity: [1x1 struct]
>> Z(2).x_values.increment
ans =
9.765625e-05
>> Z(2).number_of_values
ans =
4122210
You can easily extend this to work over multiple directories. You can add bells and whistles yourself, such as sorting based on the original fieldnames, etc. The test files are attached.
その他の回答 (3 件)
jonas
2018 年 8 月 30 日
編集済み: jonas
2018 年 8 月 30 日
As you've already read, do not. You already have your data in a struct, which is much more convenient than indexed variables. You can use dynamic field names to call your data from the struct. It's one of the common options for avoiding the infamous eval function.
Basic syntax is:
F='Signal_000'
out=MyStruct.(F)
It may however be even more convenient to put your data in a cell array, in which case you can use struct2cell
0 件のコメント
RichardB
2018 年 8 月 30 日
1 件のコメント
jonas
2018 年 8 月 30 日
編集済み: jonas
2018 年 8 月 30 日
I mean, you already received several solutions for how to process your data. However, the actual structure of the data remains a bit ambiguous so it's difficult to give you more specific, detailed, solutions. If you tell us exactly what you want to do and upload the data, then we can give you some actual useable code.
It's difficult for us, who have no knowledge of your background, to understand what "acceleration data for turbo 1 in the Z axis" actually means and how to help you find it.
RichardB
2018 年 8 月 30 日
3 件のコメント
Stephen23
2018 年 8 月 31 日
編集済み: Stephen23
2018 年 8 月 31 日
"Really, I'm not looking to write very efficient code. I just need a way to take a 100 odd struct files and turn them into 100 simple arrays (row) with a useful name I can recognize."
Whether you are just investigating your data or trying to write an efficient tool for processing lots of files, it does not change the fact that magically accessing variable names is a bad idea. Data imported using magically named variables cannot be accessed easily in a loop, which is why it forces you to continue to use slow and complex method to access your data. In contrast, when you import your data using better methods, then accessing it is simpler, and there are lots of neat tools that work on whole arrays that help you to process your data, which only work on an array and not on lots of separate variables.
The upshot is, this is not just about making more efficient code, as you simply dismissed earlier, but also about making accessing your data easier, and means you can use lots of tools that will help you to analyze your data.
" Maybe figuring out how to load the *.mat file and search for the data I want would help, but I don't understand how that would work given how it is structured."
It would help. To start with, you need to load the files into output variables:
S = load(...)
What to do next depends on exactly what the .mat files contain. It would help us a lot, if you accurately described how you get the data, in particular:
- is the data stored in one .mat file, or lots of them?
- how many variables are stored in each .mat file?
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!