Sort Columns by pairs
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Stephen23
2023-4-11
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
采纳的回答
Ran Yang
2023-4-11
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!