how to read only last 2 columns of csv format file?
조회 수: 11 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (2개)
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'})
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???
댓글 수: 0
Image Analyst
2022년 10월 9일
편집: Image Analyst
2022년 10월 9일
Try this:
filename='plot.csv';
data= readtable(filename)
x = data.b1
% Get real component from column 4.
realValues = data{:,end-1};
% Get imaginary component from column 5.
imaginaryValues = data{:,end};
complexValues = realValues + 1i * imaginaryValues
댓글 수: 6
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
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 Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!