How to open data file by browsing it - MATLAB GUI

16 ビュー (過去 30 日間)
Ekin
Ekin 2013 年 8 月 27 日
回答済み: Ka Mirul 2017 年 11 月 20 日
I can open my data file with this code:
fp= fopen('ecg.txt','rt');
fgets(fp);
A = textscan(fp, '%f');
fclose(fp);
result=A{:};
%....Function....
But it can only open the ecg.txt file. how can i open another data file by browsing it? I tried this one:
[filename1,filepath1]=uigetfile({'*.txt*','Text Files'},...
'Select Data File 1');
cd(filepath1);
opener1=load([filepath1 filename1]);
fp= fopen('filename1','rt');
fgets(fp);
A = textscan(fp, '%f');
result=A{:};
%....Function....
But getting errors and can't fix. Can anyone help? Thanks in advance!

採用された回答

Ekin
Ekin 2013 年 8 月 27 日
Alright i solved this problem:
[filename1,filepath1]=uigetfile({'*.txt*','Text Files'},...
'Select Data File 1');
cd(filepath1);
fp= fopen(filename1);
fgets(fp);
A = textscan(fp, '%f');
fclose(fp);
result=A{:};
Now it is working.

その他の回答 (3 件)

Iain
Iain 2013 年 8 月 27 日
編集済み: Iain 2013 年 8 月 27 日
If you use:
cd(filepath1);
You shouldn't need to use "filepath1" again.
opener1=load([filename1]); %reads matlab files - if you open it with "fopen", you don't need to use this line.
Remember to close the file once you've read it.
AND, importantly, since you change directory, your code might be no longer accessable. You need to ensure that your code is on the matlab path.
  2 件のコメント
Ekin
Ekin 2013 年 8 月 27 日
I changed to this one:
[filename1,filepath1]=uigetfile({'*.txt*','Text Files'},...
'Select Data File 1');
% cd(filepath1);
opener1=load([filepath1 filename1]);
fp= fopen('filename1','rt');
fgets(fp);
A = textscan(fp, '%f');
fclose(fp);
result=A{:};
But i get this error on fgets function:
Error using fgets Invalid file identifier. Use fopen to generate a valid file identifier.
Iain
Iain 2013 年 8 月 27 日
OK, try:
fp = fopen([filepath1 '\' filename1],'rt)

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


Ka Mirul
Ka Mirul 2017 年 11 月 20 日
I found a video that help me, it is about creating GUI to browse an image and display the image and its name. It should help you : https://youtu.be/7EmFShs5y9I

Vishal Rane
Vishal Rane 2013 年 8 月 27 日
Is there a particular reason to both load and textscan the file ? While loading use the '-ascii' argument to force loading of non-mat files. Also the quotes around filename1 in fopen must be removed.
Run your code in a try-catch to enable better debugging.

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by