How to write a script to analyse data files

Hi I have a major issue,
I try to write a script wich just needs the name of a file in a specific Directory (in my case xxx.neu files from a GPS System) and just shows me a graph.
In general I do this in command Window:
load ana1CleanUnf.neu
plot (ana1CleanUnf)
This is very easy. My first intention to write a script to automate this was:
function x = getGPSstats('*.neu')
x = load '*.neu'
x = plot ('*.neu')
end
but this gives my an error:
Error: File: getGPSstats.m Line: 1 Column: 26
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets
instead of parentheses.
Well I really don't know how to fix this, I try so long but really don't get it.

1 Comment

I forgot to mention, that I got about 10 .neu Files. And want to save the script in the folder, so I can just plot them with one line of code.

Sign in to comment.

 Accepted Answer

Rik
Rik on 16 Jun 2020
You can use dir to retrieve all the file names. And since you don't actually use the input of your function, I don't see why you would keep that.
I would also suggest writing a bit more robust code to load your data. This way the data magically appears in your workspace and it is difficult to track where each variable is coming from.

8 Comments

Well I'm a bloody beginner, and this is a task from my teacher. He told the class, that we have to solve it by ourselfes.It is required to write a script, that accepts one .neu file name and directly plot it. The script should be as easy as possible.
I really don't understand why I can get a plot in the command Window, but not when i write it as a script or a function. Do you have any idea how to fix the problem? I don't find any online lectures to help me with this problem. What should be the call for an unspecific file to become load and plotted as in my Command Windows? Do you have any suggestions?
Best Regards
I'd call uigetfile() to ask the user for the filename. I'd give you a snippet but since it's homework and you said "we have to solve it by ourselfes" I'll let you do it. It's very simple. And you have to pass your "x" data (actual numbers) into plot(), not a character string (filename).
thats a good hint, but unfortunatly not that was is required. The script should only call a plot... Noting else. The script called: getGPSstats(ana1CleanUnf.neu) shoul'd call the Data, load it from the file into Matlab and plot it.
There should be nothing else. And our teacher told us, if we don't understand the way, we should ask the community so that everyone, who has the same problem, can get a solution.
I've tried to do it a bit different but also don't get it right:
function x = getGPSstats('name')
% The first command is to navigate
% to the directory with the files
cd ~/Desktop/UNI/Matlab/Ex4_Data
%now load the requested file 'name.neu'
x = load ('name')
% and finally plot it
plot ('name')
end
But this gives me also an error
A bit of a guess:
function x = getGPSstats(name)
% The first command is to navigate
% to the directory with the files
cd ~/Desktop/UNI/Matlab/Ex4_Data
%now load the requested file 'name.neu'
x = load (name)
% and finally plot it
plot (x)
end
The the line below should work (assuming load understands your data file and the file is in the hard-coded folder)
getGPSstats('ana1CleanUnf.neu')
Man, you're great! Props to you! Thank you very much! Sometimes it's just a little bit of missunderstanding wich leads into total confusion. Not the script givs me the directly the Plot - that's great! But unfortunatly it also list up all the 4000 lines of the .neu file... even though I used the semicolon at the end of the lines. Do you know why?
Hope I don't look to stupid.
getGPSstats('ana1CleanUnf.neu');
function x = getGPSstats(name)
% The first command is to navigate
% to the directory with the files
cd ~/Desktop/UNI/Matlab/Ex4_Data
%now load the requested file 'name.neu'
x = load (name);
% and finally plot it
plot (x)
end
Even like that?
Thank you! It was the semikolon ath the End of the function to call it, now everything works fine!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!