Making a nice and small 'Loding Data' Script
古いコメントを表示
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
2020 年 4 月 10 日
You need to separate your tasks. Most programs you write should have this structure:
- get input (either from the user or from another function)
- validate input (parse the input and check if it is correct, throw an error if it isn't)
- do actual calculations
- prepare results for output (may depend on input, depending on your function)
You are skipping step 2, which is not a smart idea, because you can never trust user input.
In this case however, the issue is due to the contents of the file. You are trying to use load on a text file, which can work. What you need to do is verify that your file can actually be read by load. If the file is not in the correct format, you either need to write code that will read your file in a different way, or throw an error that tells the user to pick a different file.
Your code needs to work outside of the context of a GUI before you can put it in a GUI.
Wenzel Gaßner
2020 年 4 月 10 日
Rik
2020 年 4 月 10 日
The load is generally meant to read .mat files. If you want to read .txt files with it you will have to check the documentation for the specific syntax it accepts.
If you share an example file I could write some code that you can adapt. You can use the paperclip icon to attach files.
Wenzel Gaßner
2020 年 4 月 10 日
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
2020 年 4 月 10 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で App Building についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!