I am new to Matlab and i am searching for the correct code to make a plot push button into GUI after loading data from a file (another push button) .The plot i wanna do is the following but i stuck in callback functions etc
1 次查看(过去 30 天)
显示 更早的评论
a=load('data.txt'); b=sortrows(a); plot (b(2:1000,1),b(2:1000,2));
2 个评论
回答(2 个)
TastyPastry
2016-5-27
So you need two callback functions in order to do this. The first callback will load your data and the second will plot your data.
You can set callbacks like this:
set(handles.loadData,'callback',@loadDataFcn);
set(handles.plot,'callback',@plotFcn);
Then your load data callback will look like this:
function loadDataFcn(varargin)
handles = guidata(gcf);
handles.myData = load('myfile');
guidata(gcf,handles);
end
Plotting:
function plotFcn(varargin)
handles = guidata(gcf);
axes(handles.axes); %this brings the axes into focus
plot(handles.myData);
end
You can adjust the figure handle of your main GUI window to include a "myData" field so you can more easily pass data.
6 个评论
Image Analyst
2016-5-28
Well again, I have a different opinion than Tasty. GUIDE is definitely not the best, or slickest dialog box utility - not in a class with Microsoft Studio by a longshot - but for anything more than the simplest GUI, I think it's the way to go. My apps typically have about 20 - 30 controls on them, and if I were to try to build it myself, setting the height, width, location, font size, font weight, string, value, min, max, step size, etc. it would be tedious beyond belief. I'd have to hand code in hundreds of properties. If you want to go beyond the very basics in MATLAB you'll have to learn how to use GUIDE or App Designer. Fortunately it's not hard and you can master it in a few minutes after watching the tutorial link I just gave.
Image Analyst
2016-5-27
I disagree. I think you should do it all in one callback, not two. Have one button that says "Load data..." and then call load() and plot() all in the same callback, unless there is some strange reason why you'd want to load the data and NOT plot it. I mean, what is the purpose for making this into a two-step procedure when it doesn't need to be?
The reason why your code didn't work was because the badly-named "a" is actually a structure - that's what load returns. And hanging off the structure are all the variables you saved - they're fields/members of the structure. So to get them you'd do this:
storedStructure = load(matFileName);
unsortedData = storedStructure.a; % Extract "a" from the structure into a variable called "unsortedData"
sortedData =sortrows(unsortedData);
x = sortedData(2:1000,1);
y = sortedData(2:1000,2);
plot(x, y, 'LineWidth', 2, 'MarkerSize', 10);
grid on;
title('Sorted Data', 'FontSize', 20);
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!