How do i specify variable names using importfile?

3 views (last 30 days)
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:
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)
% 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}));
Alternatively, how can I specify the variable names when I call this function?
filenames = 'smooth.txt'
Or can I somehow convert the string to a variable? I tried
varname = genvarname(filenames)

Answers (0)


Find more on Data Import and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by