Importing and reading txt file in GUI
25 ビュー (過去 30 日間)
古いコメントを表示
hello i'm new in MATLAB GUI ,i just wanna know how to import a txt file in GUI and then use it for calculations
my Gui contains 2 pushbutton and a static text for displaying the result ,if any one can help i will be grateful
the file i want to import is a txt file with 2 columns "TimexValues" with 8000 data pts
thank you.
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)
[filename pathname] = uigetfile({'*.txt'},'File Selector');
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
0 件のコメント
採用された回答
Jan
2019 年 2 月 25 日
編集済み: Jan
2019 年 2 月 25 日
function pushbutton1_Callback(hObject, eventdata, handles)
[filename, pathname] = uigetfile({'*.txt'},'File Selector');
if ~ischar(filename)
return; % User aborted the file selection
end
file = fullfile(pathname, filename);
[fid, msg] = fopen(file, 'r');
if fid == -1
error(msg);
end
Data = fscanf(fid, '%g %g\n', [2, inf]); % Or how your file is formatted
fclose(fid);
handles.Data = Data;
guidata(hObject, handles); % Store updated handles struct in the GUI
end
function pushbutton2_Callback(hObject, eventdata, handles)
plot(handles.Data); % Or what ever you want to do
end
2 件のコメント
Jan
2019 年 2 月 26 日
An essential programming concept is to separate program, data and GUI strictly. Then you can call the actual function e.g. in a batch method also without the need to change the code. This means, that you use the GUI only to determine the parameter or choose a file, and call a dedicated function to perform the processing. In consequence, you do not have to consider any GUI details in the acual function and all you do there is "matrix, vectors and simple calculations". The results can be replied as output and displayed in the GUI again.
In the above example, a clean way is:
function pushbutton2_Callback(hObject, eventdata, handles)
Data = handles.Data;
Result = YourProcessing(Data);
plot(handles.axes1, Result);
end
% And in another file:
function Result = YourProcessing(X)
Result = mean(X .* X); % Or whatever
end
This is a bit artificial here, but it demonstrates the benefits of a modular programming style: Inside the GUI code, you do not care about the processing, and inside the processing the GUI is not relevant. This allows for running e.g. an automatic unit-test of te processing without a need for an interaction with the user:
Result = YourProcessing(ones(3, 3));
if ~isequal(Result, [1,1,1])
error('Test failed')
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!