Sort Columns by pairs

조회 수: 3 (최근 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 CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by