How to import text file in GUI and link it with another .m program

2 ビュー (過去 30 日間)
Lau Jit Sung
Lau Jit Sung 2020 年 5 月 11 日
コメント済み: Mehmed Saad 2020 年 5 月 11 日
I just wrote a program on importing text file in GUI, and I am not sure whether it really reads the file
% --- Executes on button press in datum1.
function datum1_Callback(hObject, eventdata, handles)
% hObject handle to datum1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename1 pathname1]=uigetfile({'*.txt'},'File Selector');
datum1=strcat(pathname1, filename1);
set(handles.datum1txt,'String',datum1)
% --- Executes on button press in datum2.
function datum2_Callback(hObject, eventdata, handles)
% hObject handle to datum2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename2 pathname2]=uigetfile({'*.txt'},'File Selector');
datum2=strcat(pathname2, filename2);
set(handles.datum2txt,'String',datum2)
After that, I want to link this program to a file called geodesycode.m with a push of a button.
  2 件のコメント
Rik
Rik 2020 年 5 月 11 日
What makes you think it read the file? You are only storing the paths to a file in the String property of two objects.
What is the input of that function? The file names or the contents of those files?
Lau Jit Sung
Lau Jit Sung 2020 年 5 月 11 日
so, how can I make the code to read the file?

サインインしてコメントする。

採用された回答

Mehmed Saad
Mehmed Saad 2020 年 5 月 11 日
編集済み: Mehmed Saad 2020 年 5 月 11 日
you have the filename and pathname, build full file name from parts
full_filename1 = fullfile(pathname1,filename1);
Use fopen to open or obtain information about file
fileID = fopen(full_filename1);
select formatspec (currently i am setting it to string)
formatSpec = '%s'; %Read all characters excluding white spaces
Now scan file with your defined formatspec using fscanf
A = fscanf(fileID,formatSpec);
Now add this to you gui handle
set(handles.datum1txt,'String',A)
and donot forget to close the file at the end
fclose(fileID)
Edit: You can also use textscan instead of fscanf
  3 件のコメント
Lau Jit Sung
Lau Jit Sung 2020 年 5 月 11 日
Hi sir, sorry to bother again what if I wanna include the blank spaces
formatSpec = '%s'; %Read all characters excluding white spaces
is there anything I should do? Thank you
Mehmed Saad
Mehmed Saad 2020 年 5 月 11 日
Use textscan instead of facanf See textscan help You have to provide three inputs 1. File id 2. format spec ('%s') 3. Delimeter

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by