writing multiple variables with eval into excel spreadsheet
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
Hi, I am trying to write final variables that was analyzed 'eval' function into excel spreadsheet. Attached data contains 3 subjects folder with 3 trials each, and data table that I want to organize. When I run this matlab script that I made, you are able to see final variables what I want to export. But I had no idea how to export nicely (not manually) these variables into data table in the excel that I finally use.
I would really appreciate somebody help me to figure this issue.
Thanks.
1 Comment
Bob Thompson
on 28 Sep 2018
How do you want the exported data organized? Multiple excel files? Multiple sheets in a single file? Blocked together?
Do you know how to use the xlswrite() command?
Accepted Answer
"But I had no idea how to export nicely..."
You can't.
Doing anything "nicely" is impossible with that code.
Because you forced yourself into writing slow, complex, buggy, obfuscated code. Read this to know why:
Because you used eval to perform every single data manipulation, with lines like this:
eval(sprintf('[%s_%s_num %s_%s_char %s_%s_raw] = xlsread(''./%s/%s.csv'');',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
eval(sprintf('[frame,NoColumn]=size(%s_%s_num);',SubjectName{i},Order{j}));
eval(sprintf('%s_%s_Pos=%s_%s_num(15:end,1:%d);',SubjectName{i},Order{j},SubjectName{i},Order{j},NoColumn));
Start again. Do NOT use that code. There is no way you can ever do anything "nicely" after doing that. Using eval like that is like shooting yourself in the knee, and then asking us how to run a marathon: what advice do you expect to get, apart from "do not shoot yourself in the knee" ?
"I just wanted to ask what would be best way to export this condition"
The "best way" is to write better code (in the sense simpler, neater, more efficient) without using eval. Probably some simple indexing (of a ND array, a cell array, a structure, a table, etc) would be the key, although using structure fieldnames or a table are also options to consider.
Let me get you started:
SubjectName = {'C01','C02','C03'};
Order = {'Gait1','Gait2','Gait3'};
NR = numel(SubjectName);
NC = numel(Order);
num = cell(NR,NC); % preallocate
str = cell(NR,NC); % preallocate
raw = cell(NR,NC); % preallocate
pos = cell(NR,NC); % preallocate
for ii = 1:NR
for jj = 1:NC
fnm = fullfile('.',SubjectName{ii},sprintf('%s.csv',Order{jj}));
[num{ii,jj},str{ii,jj},raw{ii,jj}] = xlsread(fnm);
...
end
end
Note the indexing. Note no eval anywhere. This code will be much easier to work with (just use indexing), will be less buggy, and will be much much more efficient.
9 Comments
Houn JH
on 29 Sep 2018
thank you so much your advise. you awake me. I used to use eval function to analysis those variables but I have to change my mind. I am not familiar with the code you made, if you possible could you help me to finish that code? Appreciate!!
"I am not familiar with the code you made,..."
My answer uses cell arrays and indexing. Nothing complex, and easy to learn:
"...if you possible could you help me to finish that code?"
Instead of using those complex variable names and awful eval like this:
eval(sprintf('[%s_%s_num ...] = ...',SubjectName{i},Order{j},...));
you just have to access one of those cell arrays with indexing, e.g.:
num{ii,jj}
Try it yourself. I am sure you can do it. For example, the next two lines in the loop would be:
[frame,NoColumn] = size(num{ii,jj});
pos{ii,jj} = num{ii,jj}(15:end,1:NoColumn);
Although probably you could replace both of those line with just one line:
pos{ii,jj} = num{ii,jj}(15:end,:);
Hi, Thank you for your comment. I am trying to change my code into without eval, but I have trouble to finish and get the values. If you don't mind, could you help me to change to the code what you mentioned? Attached is the one section of the copied code that I used with eval.
Appreciate your all the comment.
Stephen23
on 2 Oct 2018
@Houn JH: please upload your new code attempt as well.
Houn JH
on 2 Oct 2018
Hi, I just uploaded the code with eval. Appreciate!
@Houn JH: please upload your new code attempt as well (i.e. the one which you are writing that does not use awful eval). To be honest, I don't really understand your complex, obfuscated eval-based code at all. For example:
eval(sprintf('%s_%s_Pos=%s_%s_num(15:end,1:%d);',SubjectName{i},Order{j},SubjectName{i},Order{j},NoColumn));
...
eval(sprintf('%s_%s_1stnum=%s_%s_Pos(1,1);',SubjectName{i},Order{j},SubjectName{i},Order{j}));
...
eval(sprintf('%s_%s_RIC1=round(%s_%s_num(3,4)*100)-%s_%s_1stnum+1;',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
eval(sprintf('%s_%s_RTO=round(%s_%s_num(4,4)*100)-%s_%s_1stnum+1;',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
eval(sprintf('%s_%s_RIC2=round(%s_%s_num(5,4)*100)-%s_%s_1stnum+1;',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
eval(sprintf('%s_%s_LIC1=round(%s_%s_num(6,4)*100)-%s_%s_1stnum+1;',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
eval(sprintf('%s_%s_LTO=round(%s_%s_num(7,4)*100)-%s_%s_1stnum+1;',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
eval(sprintf('%s_%s_LIC2=round(%s_%s_num(8,4)*100)-%s_%s_1stnum+1;',SubjectName{i},Order{j},SubjectName{i},Order{j},SubjectName{i},Order{j}));
So the intention seems to be that %s_%s_Pos contains all of the numeric data, and the badly named s_%s_1stnum should contain the first frame number. So far, okay. However the following six lines make little sense:
'%s_%s_RIC1=round(%s_%s_num(3,4)*100)-%s_%s_1stnum+1;'
Why are you subtracting the frame number from some of the data values (in seconds)? Those names imply some relationship to the columns of the data, which do indeed have headers prefixed with L and R.
I have no idea how to write your code in a simpler way without eval, because I have no idea what you are trying to do.
Houn JH
on 2 Oct 2018
Thanks for your comments. The reason I wanted to take certain columns is that I will only analyze certain variables named Hip or Knee angles at the X-axis. Also, I wanted to cut frames from when legs hit ground to foot-off. This is markers position data during walking trials, so I need to cut frames for only stance phase.
"Thanks for your comments. The reason I wanted to take certain columns ..."
Sure, but it is not clear how your algorithm should work.
"I wanted to cut frames from when legs hit ground to foot-off..."
What does "cut frames" mean?
Please explain a little bit more about the algorithm or steps that you are trying to perform, and we will see if we can write it properly.
Houn JH
on 3 Oct 2018
When you look at the string at the 13 row, you will find variables character, for example, “NewSubject:RKneeAngles” or “NewSubject:RHipAngles”. I wanted find these variables values (column) within certain rows (from Foot Strike to Foot off).
When you look at the 4th column and 4 -9 rows in the string, you will find where is the foot strike and foot-off for both legs. But, these motion data collected with 100 hz, so I multiplied 100 with this time (s), so then I can find exact frame at the 1st column. So, that’s the reason I defined the Events (foot strike, foot-off) ahead of analysis. In this way, I wanted find maximum values of these variables for every 3 trials for all subjects, and then wanted export to excel spread sheet in the table the way I uploaded for the first time.
More Answers (0)
Categories
Find more on Variables in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)