Write a function wich oly needs a file input as argument

2 visualizaciones (últimos 30 días)
Niklas Reinshagen
Niklas Reinshagen el 22 de Mayo de 2021
Respondida: Image Analyst el 22 de Mayo de 2021
function getGPSstats(file)
%cd c:\Users\NMR\Desktop\Ex4_Data;
data = load(file);
% delete the error columns
data(:,7:9)=[];
% create a single vector for ech of the six remaining columns
decYear = data(:,1);
intYear = data(:,2);
dayNum = data(:,3);
eastPos = data(:,5);
vertPos = data(:,6);
% plot the data
plot(data);
I want to write a function wich acepts a .neu file from several GPS Stations. every file has about 4000x9 structure, the last three columns are error columns, so i delete them to save a bit storage.
But when I want to seperate the columns like this
decYear = data(:,1);
intYear = data(:,2);
dayNum = data(:,3);
eastPos = data(:,5);
vertPos = data(:,6);
Matlab told me "The value assigned to .... might be unused
So my function is stored as ans and get plotted at the end. So i wonder what is the issue of the code. I want to have a Matrix called data and modify it, but i ony get ans
*I have to correct myself when i try to call the function like getGPSstats('gpsdata.neu') I don't even get an ans, just the plot.

Respuestas (2)

David Fletcher
David Fletcher el 22 de Mayo de 2021
Editada: David Fletcher el 22 de Mayo de 2021
It's a warning that you have declared variables (decYear, intYear, dayNum etc.) and not used them anywhere in the code. The code will still run, but obviously if the variables aren't ever used, then there isn't much point declaring them in the first place. And yes, all the variables with be local to the function, if you do not declare any output then all you will get out of it will be the plot.
  5 comentarios
Niklas Reinshagen
Niklas Reinshagen el 22 de Mayo de 2021
soryy I don't saw you gave me an example i try it imediatly
David Fletcher
David Fletcher el 22 de Mayo de 2021
You said you got the plot at the end in your original message: if you got the plot then the data must have loaded into the variable. And if there were no error messages, all the variables derived from data were created, but as I said, unless you declare the variables as function outputs then they will all disappear when the function exits.
function [decYear, intYear, dayNum, eastPos, vertPos]=getGPSstats(file)
%cd c:\Users\NMR\Desktop\Ex4_Data;
data = load(file);
% delete the error columns
data(:,7:9)=[];
% create a single vector for ech of the six remaining columns
decYear = data(:,1);
intYear = data(:,2);
dayNum = data(:,3);
eastPos = data(:,5);
vertPos = data(:,6);
% plot the data
plot(data);
end
Then call the function with
[decYear, intYear, dayNum, eastPos, vertPos]=getGPSstats(filename)
and you will get the variables as function outputs

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 22 de Mayo de 2021
Does this work:
If not, attach the .neu file (zipped up) so we can try it.
clc; % Clear command window.
fprintf('Running %s.m ...\n', mfilename);
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
workspace; % Make sure the workspace panel is showing.
folder = 'c:\Users\NMR\Desktop\Ex4_Data';
fullFileName = fullfile(folder, 'gpsdata.neu');
data = getGPSstats(fullFileName);
% Now do something else with data.
fprintf('Done running %s.m\n', mfilename);
%===============================================================================
function data = getGPSstats(fullFileName)
data = []; % Initialize.
if ~isfile(fullFileName)
errorMessage = sprintf('Error: the file does not exist:\n%s', fullFileName);
fprintf('%s\n', errorMessage);
uiwait(errordlg(errorMessage));
return;
end
%cd c:\Users\NMR\Desktop\Ex4_Data;
data = load(fullFileName); % Maybe use readmatrix() or importdata() instead?
% Delete the error columns.
data(:,7:9)=[];
% Unused code below -- commented out.
% % Create a single vector for ech of the six remaining columns
% decYear = data(:,1);
% intYear = data(:,2);
% dayNum = data(:,3);
% eastPos = data(:,5);
% vertPos = data(:,6);
% Plot the data
plot(data, '-', 'LineWidth', 2);
grid on;
fontSize = 18;
[folder, baseNameNoExt, ext] = fileparts(fullFileName);
title([baseNameNoExt, ext], 'FontSize', fontSize);
xlabel('x', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
end

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by