How do I update the workspace while a script is running in MATLAB?

13 visualizaciones (últimos 30 días)
Sean Byrne
Sean Byrne el 20 de Abr. de 2017
Editada: Jan el 20 de Abr. de 2017
Here's the Problem...
I'm running a section of script that reads in a file and takes certain variables form that file (Height, Weight, Gender, etc) and stores them in my workspace. However occasionally if the file it's reading from is corrupt or not formatted correctly it won't read in the values quite right.
So what I have set up is a check of the variables before I continue to the larger part of my script. What I can't seem to do though is edit the variables in the workspace while the script is running. If I use functions like workspace it only displays the workspace after the script has come to an end. I want to be able to edit it during the process.
Somewhere in the below if statement if I select 'No' I want to be able to go in and edit my Subject Variables before the rest of the script continues on:
Thanks for the Help!!
AnatOK = menu('Are Subject Values ok?', 'Yes', 'No');
if AnatOK == 2
workspace
display (Subject)
end
Below is the full code I'm running:
folder=cd;
[mp,path] = uigetfile('*.mp','Select mp file','G:\Combined Database\SS Movement Assessment Database\5_Participant List Dynamic Modelled');
Subject = mp(1:end-3);
mp.filepath = [path mp];
Extracted = importdata(mp.filepath);
HeightRow = find(~cellfun('isempty',strfind(Extracted.textdata,'Height')));
Subject.Height = Extracted.data(HeightRow,1);
BodyMassRow = find(~cellfun('isempty',strfind(Extracted.textdata,'BodyMass')));
Subject.BodyMass = Extracted.data(BodyMassRow,1);
GenderRow = find(~cellfun('isempty',strfind(Extracted.textdata,'$Female')));
if Extracted.data(GenderRow,1) == 0
Subject.Gender = 'male';
elseif Extracted.data(GenderRow,1) == 1
Subject.Gender = 'female';
end
dlgTitle = 'Dominant Leg';
dlgQuestion = 'Left or right?'
Subject.Leg = questdlg(dlgQuestion,dlgTitle,'Left','Right','Yes');
display(Subject)
AnatOK = menu('Are Subject Values ok?', 'Yes', 'No');
if AnatOK == 2
workspace
display (Subject)
AnatOK = menu('Are Subject Values ok?', 'Yes', 'No');
if AnatOK == 2
error('Fix MP file structure')
end
end
Prep Anatomlical values for output
ANAT = double(struct2dataset(Subject));
fprintf('\t\t\t MP IMPORTED\n');
%%Continue to c3d
Continuec3d = menu('Continue to c3d?', 'Yes', 'No');
if Continuec3d == 1;
run SidestppingOutputs
end

Respuestas (1)

Jan
Jan el 20 de Abr. de 2017
Editada: Jan el 20 de Abr. de 2017
Don't do this. A program, which relies on magic poofing of variables by the user, cannot work reliably. The results are not reproducible and the handleing of the program is awful.
Better move the code to import the MP-files to a dedicated function and handle problems by a try/catch block. Or check for missing data directly:
HeightRow = find(~cellfun('isempty',strfind(Extracted.textdata,'Height')));
if isempty(HeightRow)
Subject.Height = Inf; % Or open a dialog
else
Subject.Height = Extracted.data(HeightRow,1);
end
If this code is used for dozens of persons, prefer a user friendly GUI: Import all available data, display it in uicontol fields, perhaps a dropdown menu for the gender. Then update the MP-files accordingly such that the next run of the code will get the same results.

Categorías

Más información sobre Workspace Variables and MAT Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by