How do i specify variable names using importfile?

3 Ansichten (letzte 30 Tage)
Alex
Alex am 19 Nov. 2018
I used the import data wizard to import data from a text file called smooth. A variable is created called smooth. Column one is altitudes. Column 2 are the corresponding measurements. If I have a text file with a different name (e.g. smooth2.txt) the variable created is called smooth2 so I can't use:
Alt=smooth(:,1);
Meas=smooth(:,2);
or the code doesnt work as soon as I try to use it for smooth2.txt. How can I specify the Alt and Meas variables in the following code?
function importfileIRIasia(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read
% Auto-generated by MATLAB on 19-Nov-2018 14:23:21
% Import the file
rawData1 = importdata(fileToRead1);
% For some simple files (such as a CSV or JPEG files), IMPORTDATA might
% return a simple array. If so, generate a structure so that the output
% matches that from the Import Wizard.
[~,name] = fileparts(fileToRead1);
newData1.(genvarname(name)) = rawData1;
% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin('base', vars{i}, newData1.(vars{i}));
end
Alternatively, how can I specify the variable names when I call this function?
filenames = 'smooth.txt'
[Alt,Meas]=importfileIRIasia(filenames)
Or can I somehow convert the string to a variable? I tried
varname = genvarname(filenames)
Alt=varname(:,1);
Meas=smooth(:,2);
Cheers,
Alex

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by