how to read only last 2 columns of csv format file?
19 views (last 30 days)
Show older comments
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 Comments
Answers (2)
dpb
on 6 Oct 2022
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 Comments
Image Analyst
on 9 Oct 2022
Edited: Image Analyst
on 9 Oct 2022
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 Comments
dpb
on 9 Oct 2022
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
on 9 Oct 2022
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);
See Also
Categories
Find more on Graphics Object Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!