How to insert cell array into a workspace structure?

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 Comment

"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.)

Sign in to comment.

 Accepted Answer

"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 Comments

I completely agree with you. Please allow me to elaborate a bit more and then I explain how, with your advice, I am changing my analysis process.
1) There are so many workspace variables because I am loading in .mat files. Each .mat file contains about 10-15 variables. So my workspace is occupied with only a 10-15 variables at a time. I was provided these mat files in these format hence I never thought about changing them.
2) I had not considered loading it into a structure already, which I admit I should've thought off.
So, following your advice, here is my solution.
s = load(....)
s =
struct with fields:
jane_doe_155: [1×1 struct]
john_doe_179: [1×1 struct]
.
.
Upon fitting the raw data, I am running the following piece of code:
f1 = 'jane_doe_155'; % this string is updated in each loop during analysis to match the subject name
f2 = 'proc';
f3 = 'fitData';
fieldNames = {f1 f2 f3}
s = setfield(s, fieldNames{:}, tempFitData);
I have written a loop to load all of my .mat data files into an structure ' s' and carry on the same analysis. Do you think use of setfields is optimum in this scenario or should I look into improving this further?
Many thanks for taking the time to read my problem and reply quickly.
@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.
Many thanks Stephen.

Sign in to comment.

More Answers (1)

Categories

Asked:

on 29 Nov 2017

Commented:

on 30 Nov 2017

Community Treasure Hunt

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

Start Hunting!