필터 지우기
필터 지우기

Making a nice and small 'Loding Data' Script

조회 수: 2 (최근 30일)
Wenzel Gaßner
Wenzel Gaßner 2020년 4월 10일
댓글: Rik 2020년 4월 12일
Hey,
I am currently trying to write a script that should be at the beginning of a larger one.
The user would run the script and be asked by the program:
-Where is the file ?
-Filename?
-other (for later)?
The script should then load the requested file into Matlab. It is measurement data in a .txt file.
I searched a lot and tried it like this:
But it doesn't work as hoped
box = msgbox('Hello, blablabla');
%set(box, 'position', [500 500 200 100]);
pause(1.2)
Folder = uigetdir();
Folder_string=string(Folder);
prompt = {'Filename:','other:'};
dlgtitle = 'blablabla';
%size of the window
width = 75;
height = 1;
dims = [height, width];
definput = {'Filename','other'};
answer = inputdlg(prompt,dlgtitle,dims,definput);
Dataname=string(answer(1,1));
other=string(answer(2,1));
%% Import
% Use Var: 'Dataname' and 'Folder' for Import:
Data = load(fullfile(Folder_string,Dataname));
After choosing the path an the filename an error appears. :(
"Error using load Number of columns on line 3 of ASCII f
ile C: \ bla \ bla \ bla \ bla \ bla \ filename must be the same as previous lines.
Error in dialog (line 45) Data = load (fullfile (Folder_s, Dataname));"
Now i dont know what to do......
  댓글 수: 6
Rik
Rik 2020년 4월 10일
That looks like a very strange file. It looks like 3 files were merged.
You can select the lines you're interested in like this:
file_contents=fileread('SBA15_Data.txt.txt');
file_contents=strsplit(file_contents,'\n');
file_contents=file_contents(6:605);
Then you only need to parse them to numeric values.
Wenzel Gaßner
Wenzel Gaßner 2020년 4월 10일
Thank you I'll try it like this tomorrow!

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2020년 4월 11일
The code below should read your file and convert it to a double array.
file_contents=fileread('SBA15_Data.txt.txt');
file_contents=strsplit(file_contents,'\n');
file_contents=file_contents(6:605);
parse_val=@(str) cellfun(@str2double,strsplit(strtrim(str),' '));
x=reshape(file_contents,[],1);
x=cellfun(parse_val,x,'UniformOutput',false);
x=cell2mat(x);
  댓글 수: 2
Wenzel Gaßner
Wenzel Gaßner 2020년 4월 12일
OK thanks,
how would I have to change your script to open the file from another folder location?
I let the user choose the location and save it in a variable:
Folder = uigetdir();
Rik
Rik 2020년 4월 12일
file_contents=fileread(fullfile(Folder,'SBA15_Data.txt.txt'));

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by