How process many structured arrays at once
Afficher commentaires plus anciens
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 commentaires
"All the posts I've seen say don't do this (was considering eval)."
That is good advice. Magically accessing variable names is how beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know more:
"How should I go about doing this?"
Simple: don't get into the situation where you need to access variable names dynamically. Presumably you did not sit and write out all the names by hand, from 000 to 157, so they must have been load-ed or generated somehow. And that is where to fix your code! Simply load into an output variable (which itself is a structure):
S = load(...)
or if creating the variables in a loop then use indexing to allocate them directly into one cell array or structure or ND array or table or ...
RichardB
le 30 Août 2018
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.
Réponse acceptée
Plus de réponses (3)
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
RichardB
le 30 Août 2018
0 votes
1 commentaire
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
le 30 Août 2018
0 votes
3 commentaires
I mean, no one is going to stop you from using eval. You can read about other options but also how to name variables dynamically here .. eval or not, I think no one can give you more details unless you describe precisely and explicitly what it is that you are trying to achieve. To me, it still remains unclear.
Looking at your data, the structure is a little bit more complicated than what it seemed before. It seems you two layers of structs and you want to extract the name of the series (stored in the field function_record.name) and the corresponding data (stored in y_values.values). So far it is clear.
What do you want to do with this data? Are you only searching for a specific name and the respective data? Do you want to plot each series of data? Do you want to structure the data in a more convenient way, so that you can access it more easily by name of the series the corresponding data?
Finally, as SC pointed out earlier, the way to read the .mat file is:
data=load('yourmatfile.mat');
This way you can start from a single struct and search it for whatever data you want without typing out every variable name by hand. Will happily help you with that if you explain in detail what to do. In my experience from this forum, well-described problems with data are solved within hours while other questions remains unanswered for days because the problem is unclear and it takes 10 iterations of answers before nailing the desirable output.
RichardB
le 30 Août 2018
"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?
Catégories
En savoir plus sur Common Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!