Effacer les filtres
Effacer les filtres

writing multiple variables with eval into excel spreadsheet

1 vue (au cours des 30 derniers jours)
Houn JH
Houn JH le 28 Sep 2018
Commenté : Houn JH le 3 Oct 2018
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 commentaire
Bob Thompson
Bob Thompson le 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?

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 29 Sep 2018
Modifié(e) : Stephen23 le 29 Sep 2018
"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 commentaires
Stephen23
Stephen23 le 2 Oct 2018
Modifié(e) : Stephen23 le 3 Oct 2018
"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
Houn JH le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by