save continously

Hello... i want to save value from "edit" GUI.. I have make it like this http://www.4shared.com/rar/jXNKKYPt/savecontinous.html, but it not suitable with my wish... I want to make value in "data1.mat" is different with "data2.mat" and so on, where the value i got from "edit" GUI when i pressed Save button... Thank you.. :)
function pushbutton1_Callback(hObject, eventdata, handles)
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
for k=1:3
% save data to mat-file
x=str2num(get(handles.edit1, 'String'));
y=str2num(get(handles.edit2, 'String'));
z=horzcat(x,y);
myfilename = strcat('data', num2str(k), '.mat');
save (myfilename,'z');
% -------------
% this code to check value of data1.mat and so on
nil1 = isequal(strcat('data', num2str(k), '.mat'), strcat('data', num2str(k+1), '.mat'));
% in this chapter, i hope value of data2.mat and so on have save
% different value,, but it's not work.. How to save them with different
% value..??
if nil1 == 1
for k = 2:3
x=str2num(get(handles.edit1, 'String'));
y=str2num(get(handles.edit2, 'String'));
z=horzcat(x,y);
myfilename = strcat('data', num2str(k+1), '.mat');
save (myfilename,'z');
end
else
% when the button pressed, the "edittext" will be empty. So, user
% can input the value again
set(handles.edit1,'String','')
set(handles.edit2,'String','')
end
% ---------------------
end
else if button_state == get(hObject,'Min')
end
end

1 Comment

Image Analyst
Image Analyst on 27 May 2012
A loop over k nested inside another loop over k is never a good idea.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 27 May 2012

0 votes

Not exactly sure what you're trying to do, but nil1 will always be false since k is never equal to k+1. So your function will clear the edit fields (edit1 and edit2) on the first iteration, and in fact every iteration. The nil1 == 1 code will never get executed.

4 Comments

Rahma Yeni
Rahma Yeni on 27 May 2012
I'm not sure to my code...
I want to make the result like this:
When i make value of x = 5, y = 2, so now, the data1.mat will contain 5, 2 and data2.mat, data3.mat will be null...
Then, when i repeated it again, x = 7, y = 4, this data will be saved in data2.mat, and now, only data3.mat is null,,, and so on...
how i can make it like that...???
Thank you before.. :)
Image Analyst
Image Analyst on 27 May 2012
Just have one loop over k. Get the initial values of x,y,z from the edit fields before the loop even begins In the loop, create the filename, then save z. At the bottom of the loop, create the new values of x,y,z, which will be used on your next pass through the loop. That pretty well spells it out. Give it a try and write back if you can't figure it out.
Rahma Yeni
Rahma Yeni on 27 May 2012
I'm sorry Sir,, I can't implement your illustration to my code.. :(
can you help me Sir...?? Please...
Image Analyst
Image Analyst on 27 May 2012
Not sure how x went from 5 to 7 and y went from 2 to 4, but see the code I posted and try to adapt it to whatever you want/need.

Sign in to comment.

Image Analyst
Image Analyst on 27 May 2012
Try this (untested):
function pushbutton1_Callback(hObject, eventdata, handles)
folder = pwd; % or wherever you want.
try
% save data to mat-file
x=str2double(get(handles.edit1, 'String'));
y=str2double(get(handles.edit2, 'String'));
z = [x y];
for k = 1 : 3
% Save the file.
baseFileName = sprintf('data %d.mat', k);
fullFileName = fullfile(folder, baseFileName);
save (fullFileName,'z');
% Now we prepare for next time.
% Note: not really sure how x and y are
% supposed to be different each time through the loop.
x=str2num(get(handles.edit1, 'String'));
y=str2num(get(handles.edit2, 'String'));
z = [x y];
% Clear edit 2 for some reason.
set(handles.edit2,'String','')
end
% ---------------------
catch ME
errorMessage = sprintf('Error in function pushbutton1_Callback():\n%s', ME.message);
uiwait(warndlg(errorMessage));
end

Asked:

on 27 May 2012

Community Treasure Hunt

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

Start Hunting!