hi ! i am developing a gui that basically takes input from the interface processes some calculation and generated graph of the calculated data with the trigerring of push button

the thing is i am successful till date on the project now im stuck with this small problem, like i am doing some calculations and generating values right, i want to transfer these values to a text file , i tried the below code and mentioned the path of the text file
text_file_name='experiment.txt'; full_path=strcat('C:/Users/r_a192/Desktop/',text_file_name); fid = fopen(full_path,'wt'); fprintf(fid,'thetha value genrated is %f and frequency genrated is %f ',theta,fm); fclose(fid); [fid,msg] = fopen(full_path,'wt'); if(fid == -1) error(msg)
is this correct ?? do i need to make changes??i am not able to get any result, please help me i am not able to solve the problem

 Réponse acceptée

Rohith - it is not all that clear from your question what the problem is. Is the file being generated with no data? Is the file being generated with invalid data? Or is the file not being generated at all?
Please try the following:
text_file_name='experiment.txt';
directory_name='C:/Users/r_a192/Desktop';
full_path = fullfile(directory_name,text_file_name);
% open the file for writing (w) in text mode (t)
fid = fopen(full_path,'wt');
if fid<0
error(['Could not open file ' full_path]);
else
fprintf(fid,'thetha value genrated is %f and frequency genrated is %f\n',theta,fm);
fclose(fid);
end
Don't call the [fid,msg] = fopen(full_path,'wt'); again else it will overwrite the contents of the file.
If you want to append the results to the file (and so not lose the previous results), then change the fopen permission mode to append (a)
fid = fopen(full_path,'at');
Try the above and see what happens!

10 commentaires

thank you now i am able to see that the text file is being generated but content is not being added to the text file
function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
axes(handles.axes1);%selecting the axes1 to plot the graphs cla;
popup_sel_index = get(handles.popupmenu1, 'Value'); switch popup_sel_index %writing the switch case for selecting options to choose the method case 1 fw1 = str2double(get(handles.f1_input,'String')); %reading input from the text boxes fw2 = str2double(get(handles.f2_input,'String')); fw3 = str2double(get(handles.f3_input,'String')); fw4 = str2double(get(handles.f4_input,'String')); fw5 = str2double(get(handles.f5_input,'String')); fw6 = str2double(get(handles.f6_input,'String')); fw7 = str2double(get(handles.f7_input,'String')); fw8 = str2double(get(handles.f8_input,'String'));
fr1 = fw5-fw1; %caluculating the further answers fr2 = fw6-fw2; fr3 = fw7-fw3; fr4 = fw8-fw4;
% Calculate data Frx = (fr1+0.707*(fr2-fr4))/4; %using the formula to find the x and y values Fry = (fr3+0.707*(fr2+fr4))/4; Fm = sqrt((Frx)^2+(Fry)^2); theta = atand(Frx/Fry); %genrating the angle in degrees compass(handles.axes1,theta,Fm); %plotting the function that is required set(h,'LineWidth',30) %setting width of the needle in the graph
case 2
fw1 = str2double(get(handles.f1_input,'String'));
fw2 = str2double(get(handles.f2_input,'String'));
fw3 = str2double(get(handles.f3_input,'String'));
fw4 = str2double(get(handles.f4_input,'String'));
fw5 = str2double(get(handles.f5_input,'String'));
fw6 = str2double(get(handles.f6_input,'String'));
fw7 = str2double(get(handles.f7_input,'String'));
fw8 = str2double(get(handles.f8_input,'String'));
fr1 = fw5-fw1; fr2 = fw6-fw2; fr3 = fw7-fw3; fr4 = fw8-fw4;
% Calculate data Frx = (fr1+0.707*(fr2-fr4))/4; Fry = (fr3+0.707*(fr2+fr4))/4; Fm = sqrt((Frx)^2+(Fry)^2); theta = atand(Frx/Fry); compass(handles.axes1,theta,Fm);
case 3
fw1 = str2double(get(handles.f1_input,'String'));
fw2 = str2double(get(handles.f2_input,'String'));
fw3 = str2double(get(handles.f3_input,'String'));
fw4 = str2double(get(handles.f4_input,'String'));
fw5 = str2double(get(handles.f5_input,'String'));
fw6 = str2double(get(handles.f6_input,'String'));
fw7 = str2double(get(handles.f7_input,'String'));
fw8 = str2double(get(handles.f8_input,'String'));
fr1 = fw5-fw1; fr2 = fw6-fw2; fr3 = fw7-fw3; fr4 = fw8-fw4;
% Calculate data Frx = (fr1+0.707*(fr2-fr4))/4; Fry = (fr3+0.707*(fr2+fr4))/4; Fm = sqrt((Frx)^2+(Fry)^2); theta = atand(Frx/Fry); compass(handles.axes1,theta,Fm);
case 4
fw1 = str2double(get(handles.f1_input,'String'));
fw2 = str2double(get(handles.f2_input,'String'));
fw3 = str2double(get(handles.f3_input,'String'));
fw4 = str2double(get(handles.f4_input,'String'));
fw5 = str2double(get(handles.f5_input,'String'));
fw6 = str2double(get(handles.f6_input,'String'));
fw7 = str2double(get(handles.f7_input,'String'));
fw8 = str2double(get(handles.f8_input,'String'));
%t = eval(get(handles.t_input,'String')); fr1 = fw5-fw1; fr2 = fw6-fw2; fr3 = fw7-fw3; fr4 = fw8-fw4;
% Calculate data Frx = (fr1+0.707*(fr2-fr4))/4; Fry = (fr3+0.707*(fr2+fr4))/4; Fm = sqrt((Frx)^2+(Fry)^2); theta = atand(Frx/Fry); compass(handles.axes1,theta,Fm);
end text_file_name='experiment.txt'; directory_name='C:\Users\r_a192\Documents\MATLAB'; full_path=fullfile(directory_name,text_file_name);
% open the file for writing (w) in text mode (t)
fid = fopen(full_path,'wt');
if fid<0
error(['Could not open file ' full_path]);
else
fprintf(fid,'thetha value genrated is %f and frequency genrated is %f\n',theta,fm);
fclose(fid);
end;
if true
% code
end
this is my code i am able to generate the file but not able to update it with the values of theta and fm
Put a breakpoint at the line
fprintf(fid,'thetha value genrated is %f and frequency genrated is %f\n',theta,fm);
to make sure that this line is being evaluated when you press the Calculate button (or whatever it is named). Once the code is paused here, press the Step button twice (it is the button in the Editor) so that the fprintf and the fclose are evaluated. As soon as the second line has been passed, open the text file (not in MATLAB but in notepad) - do you see anything in it?
like after setting the break point at this line you specified i am not able to use the step as it is inactive
i tried what you told and opened it in notepad but there is nothing in the text file
Rohith - once the breakpoint is set, launch your GUI, do your calculations and press the button…only then will the code pause at the breakpoint and the Step button will be active.
Even better - run the GUI and just BEFORE you press the calculate button, make sure that the breakpoint still exists at the specified line...
yeah i tried the same what you have told before but still the step is inactive
What do you mean by the step is inactive? Is the button disabled? When you run the GUI and press the Calculate button, does the code pause at the line where you put the breakpoint?
If you are unused to debugging in MATLAB, then you should check the following link http://blogs.mathworks.com/videos/2010/09/02/using-debugger-to-walk-through-code/
can you tell me your email so that i can send you the code and .fig file where you can see to the problem if you have some time
Rohith - you still haven't answered my question - what do you mean by the step is inactive? If you do not know how to use the debugger then use the link that I provided in my previous comment. Being able to debug in MATLAB is an incredibly useful skill. You should take the time to learn how to do this.
I glanced at your code, and as soon as I pressed the Plot button, the software crashed at the line
set(h,'LineWidth',30)
because there is no h handle. I think that this should instead be
set(handles.axes1,'LineWidth',30);
As well, at the line of code where theta is calculated, rather than using
theta = atand(Frx/Fry);
you should probably use
theta = atan2d(Frx,Fry);
to avoid the division by zero for the case where Fry is zero (which, currently, means that theta is NaN).
I put a breakpoint at the line where the 'experiment.txt' file is created and then stepped through the code to see if data is being written to this line. Once I stepped past the line fclose(fid);, I opened the file (which was now created in the specified directory) and observed the following text
thetha value genrated is NaN and frequency genrated is 0.000000
So the writing to file does work.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by