Save ans file into a seperate file

1 view (last 30 days)
Bharg Shah
Bharg Shah on 17 Mar 2021
Answered: Jan on 18 Mar 2021
Hello,
I am trying to write a code, that automatically saves the woekspace variable "ans" into a specified variable in a loop.
Example:
I have 3 data files, that I have to read and save in seperate variable.
I first created this function to read these files.
function NACA0012forceCoeffs = import_ForceCoeff(filename, dataLines)
%IMPORTFILE Import data from a text file
% AOA0WITHOUTTAILNACA0012FORCECOEFFS0 = IMPORTFILE(FILENAME) reads data
% from text file FILENAME for the default selection. Returns the data
% as a table.
%
% AOA0WITHOUTTAILNACA0012FORCECOEFFS0 = IMPORTFILE(FILE, DATALINES)
% reads data for the specified row interval(s) of text file FILENAME.
% Specify DATALINES as a positive scalar integer or a N-by-2 array of
% positive scalar integers for dis-contiguous row intervals.
%
% Example:
% AoA0WITHOUTTailNACA0012forceCoeffs0 = importfile("C:\Users\Bharg\Desktop\G5\Ubuntu\Results_NACA 0012\ForceCoefficients_NACA_0012\AoA_0_WITHOUT_Tail_NACA_0012_forceCoeffs_0.dat", [10, Inf]);
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 17-Mar-2021 14:31:49
%% Input handling
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [10, Inf];
end
%% Setup the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 6);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["Time", "Cm", "Cd", "Cl", "Clf", "Clr"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Import the data
NACA0012forceCoeffs = readtable(filename, opts);
end
I am trying to write a for loop that creates, varaibles such as AoA_0 = (the data from the AoA_0_WITHOUT_Tail_NACA_0012__forceCoeffs.txt ) file

Answers (1)

Jan
Jan on 18 Mar 2021
I don't get what the problem is. What about:
Data{1} = import_ForceCoeff('AoA_5_WITHOUT_Tail_NACA_0012_forceCoeffs_0.txt');
Data{2} = import_ForceCoeff('AoA_10_WITHOUT_Tail_NACA_0012_forceCoeffs_0.txt');
...

Community Treasure Hunt

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

Start Hunting!