Need help about calling m file into Matlab command window/workspace ...
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
My version of the matlab is r2010b!
I have 2 m-files, the main one - it first runs in a command window (main.m), and the other one (data.m), which is called during the execution of the first - for loading data ...
main.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
clc
while 1
edit ('data.m')
disp ('After the completion of data entry, press 1,');
disp ('To change the input data, press 0');
k = input ('');
if (k == 1)
fprintf ('\ n Loading data ..... \ n \ n')
data
break;
end
end
s = a * 10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
a = [1 2 3 4 5]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
First I run main.m --- it opens data.m --- I change vector a (eg a = [10 20 30]) --- and save it (click on the save button) --- BUT INSTEAD OF LOADING NEW-Changed vector a = [10 20 30], main.m loads the previous one (a=[1 2 3 4 5] ) from data.m !!!!!
Only when I restart main.m then it loads a changed vector --- [10 20 30 ]................
How do I fix this? tnx
Nikola
1 comentario
Respuesta aceptada
Más respuestas (3)
Fangjun Jiang
el 21 de Ag. de 2011
Add a line pause(0) after the line edit('data.m') in main.m and it should work.
It forces MATLAB to refresh file/folder so the updated data.m is reflected.
3 comentarios
Fangjun Jiang
el 21 de Ag. de 2011
Something strange. Yesterday when I tried, I saw the problem which was the updated data.m didn't take effect. I added pause(0) which did make a difference. Today, when I tried, your original code worked. Without pause(0), the updated data.m did take effect. So what is your verdict? I'll find out more.
Oleg Komarov
el 21 de Ag. de 2011
In main.m:
...
s = a * 10
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data.m
...
but then you clear and you declare a independently on what's stored in data.m:
...
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear
a = [1 2 3 4 5]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
So, it did load the modified a but you cleared it and then created the old one.
Apart from this what are you trying to accomplish? The approach may be not the best one.
EDIT
Keeping your inputs separate from the script has sense only if the amount of data is relevant and even in this case .mat files are more suitable for this task.
Even if you have 30 variables, but they are compact/short, it would make sense to keep them all in a separate section but on the same .m file.
%%INPUTS
a = 1:5;
b = ...
z = ...
%%ENGINE
Or, if the user has to be continuosly prompted for inputs you could use input directly or inputdlg with default answer so that the user could accept or edit the default.
Your solution is not robust because it's enough to forget to save once that you have to repeat the editing process from the beginning.
3 comentarios
Oleg Komarov
el 21 de Ag. de 2011
What I do is:
1) in the command window: main.m
2) data.m is prompted, I edit a
3) I save data.m
4) in the command windonw: 1
a,s,k appear in the workspace all according to the modified values of a I edited in point 2.
Again, what clear is there for after you executed main.m?
And why do you redeclare a?
If this is not your problem, then take the time to show what are you really doing?
Also, see my edit for an alternative to manage your project.
Fangjun Jiang
el 21 de Ag. de 2011
I did the following test. The problem is clear. The updated data.m file didn't take effect. With or without pause(0) didn't make a difference. I must have made some mistakes.
rehash() is the right function to use. But it needs to be put after the "k=input(' ') line.
data.m contains one line: a=1
main.m
clear
while 1
edit ('data.m')
pause(0);
disp ('After the completion of data entry, press 1,');
disp ('To change the input data, press 0');
k = input ('');
if (k == 1)
fprintf ('\n Loading data ..... \n')
data
break;
end
end
s = a * 10
Run the following code. Every time data.m is brought up, edit it with the same value as Iteration (1,2,3) and save it.
diary('out.txt');
for Iteration=1:3
fprintf('Iteration=%d\n',Iteration);
main;
end
diary off;
out.txt
Iteration=1
After the completion of data entry, press 1,
To change the input data, press 0
1
Loading data .....
a =
1
s =
10
Iteration=2
After the completion of data entry, press 1,
To change the input data, press 0
1
Loading data .....
a =
1
s =
10
Iteration=3
After the completion of data entry, press 1,
To change the input data, press 0
1
Loading data .....
a =
1
s =
10
1 comentario
Oleg Komarov
el 21 de Ag. de 2011
Ok, I see the problem. In my case only the first modification is correctly taken into account BUT
I would not recommend to follow this road as I already pointed out in my EDIT.
This way of storing data and retrieving is "unnatural" and shows this unexpected behaviour, which may or may not be a bug.
Ver también
Categorías
Más información sobre Search Path 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!