i have developed a gui with a plotting area , now the problem is i need to transfer the results of the calculation that are being used in plotting to be shown in the text file so can you suggest me how to do that on clicking the plot button.

i have developed a gui with a plotting area , now the problem is i need to transfer the results of the calculation that are being used in plotting to be shown in the text file so can you suggest me how to do that on clicking the plot button.

1 Comment

Rohith - in the future, rather than copying and pasting the body of your question in to the header/title, keep the latter short and clear.

Sign in to comment.

 Accepted Answer

Your GUI button needs a callback that will be invoked whenever the user presses the button. If the button name is pushbutton1 then your GUI m file should have something like the following defined within it
% --- Executes on button press in pushbutton1.
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)
In the body of this function you will plot the data (since you have called this the plot button) and write the data to file:
% plot the data
% open a text file
fid = fopen('fileToWriteTo.txt','w');
if fid>0
% write the data to file (this may require a loop given your data set)
fprintf(fid,'formatString',dataToWriteA,dataToWriteB, etc...);
% close the file
fclose(fid);
end
The formatString in the above describes the format of the data (integers, floats, strings, etc.) and the dataToWriteX is the data to write to file. In the command window type help fprintf or doc fprintf for details on how to use this function.

9 Comments

is the text file automatically generated ? or else do we need to generate it separately ?how do you display the file that is generated?
You have to write code to populate the text file with the results from your plot. In the above code example, the text file is opened and the fprintf is there to show how you might write data to that file, but you (as the developer) must add that missing functionality in as only you know what data was used on your plot.
how do you display the text file that has been generated?
What do you want to display the text file in? Or do you mean that you want to plot the data (which you have written to file) on a axes that is embedded in your GUI? Grab the handle to the axes (from the handles structure) and just plot away
% within the same callback as above
% once all data has been written to file, get the handle to your axes on
% your GUI
haxis = handles.axes1; % assumes your axes is named axes1
% plot the data
plot(haxis,yourDataToPlot,);
Again, I don't know the data that you have calculated or what you have written to file (or how you even want to show it). In the command window, type help plot or doc plot for details on this function.
% plot the data
% open a text file
fid = fopen('experiment.txt','w');
if fid>0
% write the data to file (this may require a loop given your data set)
fprintf(fid,'thetha value genrated is %f and frequency genrated is %f ',theta,fm);
% close the file
fclose(fid);
type experiment.txt
end
with the above code we are able to write text to the file i understand that point, my query is that do we need to create a file named experiment.txt by specifying the path and then add data using above code *OR* else this above code generates the file named experiment.txt automatically
If you want to write data to a file, then you have to write the code that will do that…it won't happen automatically. So all of the code in your above comment is necessary (with the exception of type experiment.txt which just displays the file contents).
your not getting my point dude!
thank you i got the explanation regarding writing content to the text file. i was asking you about the text file, are we generating the new text file and then adding content to it or else ,are we adding content to a pre-generated text file.
Huh. I figured something like that you would have been able to determine on your own by running the code and observing the results. Look at the details for fopen (in the Command Window, type help fopen or doc fopen) or use this link http://www.mathworks.com/help/matlab/ref/fopen.html. Scroll down to the section on permission - File access type. Expand this section. Note the file access type for 'w'. It reads as follows
Open or create new file for writing. Discard existing contents, if any.
So if a file does not exist, a new one is created. If a file does exist, then its contents are cleared and it will be as if a new file has been created.
thank you!!! thank you so much for your valuable time and suggestions!

Sign in to comment.

More Answers (1)

3 Comments

Ooops - I see you just accepted an Answer, so never mind.
thank you yeah i got my problem solved nearly just its that i am not able to display the generated file using "type filename.txt"
How about system('notepad filename.txt') or something like that?

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!