How to insert cell array into a workspace structure?

8 Ansichten (letzte 30 Tage)
Abhi Ove
Abhi Ove am 29 Nov. 2017
Kommentiert: Abhi Ove am 30 Nov. 2017
Dear All, I have quite an unusual problem that I can't solve. After trying to solve it myself for a while I think I need some help. So here it goes...
My Workspace is populated with structure with unique names and several fields
whos
Name Size Bytes Class Attributes
john_doe_155 1x1 2677 struct
jane_doe_179 1x1 2895 struct
.
.
within each structure there are nested structure for rawData(raw) and processedData (proc) such that;
john_doe_155
struct with fields:
procData: [1×1 struct]
rawData: [1×1 struct]
Each set of rawData and procData contains matrix of variables. At this point in time I have fitted a model to a set off raw data. In order to make my analysis accessible to future users, I have created a temporary cell array (tempFitData) that contains few bits of the information such that ....
tempFitData = 1×5 cell array;
Now I need to insert this tempFitData into the procData structure so that in future everyone can access them. At the moment if I command;
john_doe_155.procData.fitData = tempFitData;
I get exactly what I am looking for.
john_doe_155.procData
struct with fields:
time: [6×1 double]
fitData: {1×5 cell}
However I have literally 1000's of these entry in my workspace to go through and I can not do them all individually by hand.Is there any way to automate the whole process?
Unfortunately I have to return the data set in exactly the same way so I have tried using the eval function without any success. Any help will be much appreciated.
  1 Kommentar
Stephen23
Stephen23 am 29 Nov. 2017
Bearbeitet: Stephen23 am 29 Nov. 2017
"However I have literally 1000's of these entry in my workspace to go through and I can not do them all individually by hand.Is there any way to automate the whole process?"
Yes. Write better code that does not create 1000's of variables in the workspace.
(note: "better" in the sense that the code is neater, less buggy, less complex, more secure, more efficient, and easier to debug.)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 29 Nov. 2017
Bearbeitet: Stephen23 am 29 Nov. 2017
"However I have literally 1000's of these entry in my workspace to go through and I can not do them all individually by hand.Is there any way to automate the whole process?"
Yes, there is. The solution is do not have 1000's of separate variables in your workspace.
There are basically two ways that you can get lots of variables into your workspace:
  1. calculated during a loop.
  2. loading from a data file.
But actually neither of these require that you have to create 1000's of separate variables. For example it is trivial to load data into an output variable (which is a structure) and process that:
for k = ...
S = load(...)
...
end
You can easily process the fields of S and join them into one array, or one structure, or whatever suits your processing. It all depends on what you need to do.
The most important thing is to not create lots of separate variables in your workspace, because then you make working with your data complex, slow, buggy, hard to debug, and as you have found out, difficult to make it work properly. Read this to know more:
and in particular this answer:
  3 Kommentare
Stephen23
Stephen23 am 30 Nov. 2017
@Abhi Ove: That seems like a perfectly good use of setfield to me. Remember that you can also access fieldnames dynamically:
Also note that you should accept the answer that helped you most. Accepting answers is the easiest way for you to show you appreciation to the volunteers who helped you.
Abhi Ove
Abhi Ove am 30 Nov. 2017
Many thanks Stephen.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Stephen23
Stephen23 am 30 Nov. 2017
Did you forget to accept an answer?

Kategorien

Mehr zu Structures 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