Info

This question is closed. Reopen it to edit or answer.

How I can read variables from a .txt?

1 view (last 30 days)
Christian Fernando
Christian Fernando on 21 Jul 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a GUI and I need to save some variables in a .txt to export to another platform while you can rerun the GUI with the same values at any time
My code is:
function Save_Callback(hObject, eventdata, handles)
global D F W
File=get(handles.edit1,'String');
save(File, 'D', '-ASCII','-append');
save(File, 'F', '-ASCII','-append');
save(File, 'W', '-ASCII','-append');
function Load_Callback(hObject, eventdata, handles)
global D F W
File=get(handles.edit2,'String');
  4 Comments
dpb
dpb on 23 Jul 2014
Edited: dpb on 23 Jul 2014
I can not find the example...
It's the "Load ASCII File" example.
NB in the doc for save that if you try to use load for ASCII files written by save that all variables must have same number of columns which your variables above do not--hence you canNOT use save/load in this fashion for your purposes.
For export purposes do you need to keep the shape? Does the other system know the shape/size a priori? If not it'd probably simplify that side's job to write it as a header record as well as the data and/or use a csv file or similar.
Christian Fernando
Christian Fernando on 24 Jul 2014
Use csvread or dlmread is a good solution. Thank you
D=[4 5;14 15];
F=[4 5 5;4 8 9;4 1 2];
W=[4 7 8 9 1 2 3];
File='New.txt';
dlmwrite(File,D,'delimiter','\t','precision',10)
dlmwrite(File,F,'-append','delimiter','\t','precision',10)
dlmwrite(File,W,'-append','delimiter','\t','precision',10)
a1=0; b1=size(D,1)-1;
d=dlmread(File,'\t',[a1 0 b1 size(D,2)-1]);
a1=a1+size(D,1); b1=b1+size(F,1);
f=dlmread(File,'\t',[a1 0 b1 size(F,2)-1]);
a1=a1+size(F,1); b1=b1+size(W,1);
w=dlmread(File,'\t',[a1 0 b1 size(W,2)-1]);
At the end D=d F=f & W=w

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!