Sort Columns by pairs
5 views (last 30 days)
Show older comments
I have following problem: My code imports data from one file and i want two extract two columns from it, but instead of pairs i get a list
I get:
frequenz1 frequenz2
frequen3 ...
db1 db2
db3 ...
but i want:
frequenz 1 db1
frequenz 2 db2
...
my code is the following:
%% Import data from text file
% Script for importing data from the following text file:
%
% filename: \\Dedosan001\vol1\E\EMV\Kularia\Matlabprogramm\04_Messungen\04\Ergebnistabelle_AV.txt
%
% Auto-generated by MATLAB on 06-Apr-2023 11:22:46
%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 9, "Encoding", "UTF16-LE");
% Specify range and delimiter
opts.DataLines = [30, 710];
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["Frequenz", "PK_MAXH", "Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9"];
opts.SelectedVariableNames = ["Frequenz", "PK_MAXH"];
opts.VariableTypes = ["double", "double", "string", "string", "string", "string", "string", "string", "string"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, ["Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9"], "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["Var3", "Var4", "Var5", "Var6", "Var7", "Var8", "Var9"], "EmptyFieldRule", "auto");
% Import the data
ErgebnistabelleAV1 = readtable("\\Dedosan001\vol1\E\EMV\Kularia\Matlabprogramm\04_Messungen\04\Ergebnistabelle_AV.txt", opts);
%% Clear temporary variables
clear opts
% Speichern der ersten beiden Spalten in einer neuen Datei
outputFilePath = '\\DEDOSAN001\vol1\E\EMV\Kularia\Matlabprogramm\15\1_2.asc';
fileID = fopen(outputFilePath, 'w');
fprintf(fileID, '%f\t%f\n', ErgebnistabelleAV1.Frequenz, ErgebnistabelleAV1.PK_MAXH);
fclose(fileID);
2 Comments
Stephen23
on 11 Apr 2023
The best solution is to use WRITETABLE, as Ran Yang shows. Otherwise you will need to either use a loop or concatenate those variables into one matrix, e.g.:
M = [ErgebnistabelleAV1.Frequenz, ErgebnistabelleAV1.PK_MAXH];
fprintf(fileID, '%f\t%f\n', M.'); % note the transpose
Accepted Answer
Ran Yang
on 11 Apr 2023
Store your table as a Matlab variable and then save that, instead of messing with fopen/fclose.
T = ErgebnistabelleAV1(:, {'Frequenz', 'PK_MAXH'}); % or whatever column names you need
writetable(T, 'filename.ext')
0 Comments
More Answers (0)
See Also
Categories
Find more on Text Files 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!