how to read only last 2 columns of csv format file?

16 ビュー (過去 30 日間)
ramya
ramya 2022 年 10 月 6 日
コメント済み: Image Analyst 2022 年 10 月 9 日
i have to read last 2 columns and thwn write in a+bi format
b1 column data is continoiusly repeating 1-5 with n no of data and plot graph
x axis =b1 column
y axis =a+bi
2018 a version

回答 (2 件)

dpb
dpb 2022 年 10 月 6 日
data=readtable(websave('plot.csv','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1147855/plot.csv'),'readvariablenames',1);
tXY=table(data.b1,complex(data.b2,data.C1),'variablenames',{'X','Y'})
tXY = 10×2 table
X Y _ _________ 1 -4.2-5.2i 2 -3.2+6.2i 3 -5.2+3.2i 4 -3.2-5.2i 5 -6.2-6.3i 1 -2.3-9.2i 2 2.3-6.2i 3 2.3-3.2i 4 6.2-6.3i 5 3.2-7.8i
But, MATLAB can't plot complex variables versus a real index as a line plot; it isn't clear what you have i mind here???

Image Analyst
Image Analyst 2022 年 10 月 9 日
編集済み: Image Analyst 2022 年 10 月 9 日
Try this:
filename='plot.csv';
data= readtable(filename)
data = 10×5 table
a1 a2 b1 b2 C1 __ __ __ ____ ____ 1 0 1 -4.2 -5.2 2 0 2 -3.2 6.2 3 0 3 -5.2 3.2 4 0 4 -3.2 -5.2 5 0 5 -6.2 -6.3 6 0 1 -2.3 -9.2 7 0 2 2.3 -6.2 8 0 3 2.3 -3.2 9 0 4 6.2 -6.3 10 0 5 3.2 -7.8
x = data.b1
x = 10×1
1 2 3 4 5 1 2 3 4 5
% Get real component from column 4.
realValues = data{:,end-1};
% Get imaginary component from column 5.
imaginaryValues = data{:,end};
complexValues = realValues + 1i * imaginaryValues
complexValues =
-4.2000 - 5.2000i -3.2000 + 6.2000i -5.2000 + 3.2000i -3.2000 - 5.2000i -6.2000 - 6.3000i -2.3000 - 9.2000i 2.3000 - 6.2000i 2.3000 - 3.2000i 6.2000 - 6.3000i 3.2000 - 7.8000i
  6 件のコメント
dpb
dpb 2022 年 10 月 9 日
That again will be the reason to switch to using a table and writetable (below from the "See Also" version history at above link)
Version History
Introduced in R2013b
and, unless I'm sadly mistaken, R2013b will have predated R2018x.
None of the other output routines than the new(ish) writeXXX family knows how to write complex variables excepting by writing both the real and complex parts separately and physically inserting the connecting sign and "i". Nor can you store the various data types contained in the file of both double and the complex in a single variable to write in a single call excepting in a cell array other than in a table, so it's even more of an effort.
Fail to understand your reluctance to use the tools MATLAB does provide...
Image Analyst
Image Analyst 2022 年 10 月 9 日
Try this:
fullFileName = fullfile(pwd, 'plot.csv')
dataStructure = importdata(fullFileName)
data = dataStructure.data;
x = data(:, 3);
% Get real component from column 4.
realValues = data(:, 4);
% Get imaginary component from column 5.
imaginaryValues = data(:, 5);
complexValues = realValues + 1i * imaginaryValues
% Append the complex number after the last number
% Open the file for reading in text mode.
inputFileID = fopen(fullFileName, 'rt');
% Open an output file.
outputFileName = 'plot with complex numbers.txt';
outputFileID = fopen(outputFileName, 'wt');
% Read the first line of the file.
textLine = fgetl(inputFileID); % Read and discard first line
fprintf(outputFileID, '%s, Complex Number\n', textLine);
textLine = fgetl(inputFileID); % Get next line, which will have numbers on it.
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on in the command window.
fprintf('%s, %f + i %f\n', textLine, realValues(lineCounter), imaginaryValues(lineCounter));
% Now write to the file.
fprintf(outputFileID, '%s, %f + i %f\n', textLine, realValues(lineCounter), imaginaryValues(lineCounter));
% Read the next line.
textLine = fgetl(inputFileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(inputFileID);
fclose(outputFileID);
% Open the output text file.
winopen(outputFileName);

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

カテゴリ

Help Center および File ExchangeGraphics Object Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by