フィルターのクリア

Sort Columns by pairs

1 回表示 (過去 30 日間)
Nainpreet
Nainpreet 2023 年 4 月 11 日
コメント済み: Nainpreet 2023 年 4 月 11 日
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
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
Nainpreet
Nainpreet 2023 年 4 月 11 日
Thanks that worked

サインインしてコメントする。

採用された回答

Ran Yang
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 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by