Importing and reading txt file in GUI

6 Ansichten (letzte 30 Tage)
bishop Rab
bishop Rab am 25 Feb. 2019
Kommentiert: Jan am 26 Feb. 2019
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)

Akzeptierte Antwort

Jan
Jan am 25 Feb. 2019
Bearbeitet: Jan am 25 Feb. 2019
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 Kommentare
bishop Rab
bishop Rab am 26 Feb. 2019
Thank you Jan ,one question plz, how i can manipulate data,i mean the two columns of my txt file,"Values Vs Time " data,i want to do some simple calculus like mean values,max...etc in GUI .could you suggest for me a useful source for documentation "in Mathworks or books",to understand how to code in gui,cause it's little different than Matlab "matrix,vectors..simple calc".
Jan
Jan am 26 Feb. 2019
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by