save continously

조회 수: 6 (최근 30일)
Rahma Yeni
Rahma Yeni 2012년 5월 27일
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
Image Analyst
Image Analyst 2012년 5월 27일
A loop over k nested inside another loop over k is never a good idea.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
Image Analyst 2012년 5월 27일
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
Rahma Yeni
Rahma Yeni 2012년 5월 27일
I'm sorry Sir,, I can't implement your illustration to my code.. :(
can you help me Sir...?? Please...
Image Analyst
Image Analyst 2012년 5월 27일
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.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 5월 27일
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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by