i have ascii data converted into columns and rows seperatby str2num command now i want to add all rows in orderly in one cell by using for loop can you give me ans
Show older comments

3 Comments
Dyuman Joshi
on 12 Dec 2023
Why use loop when can just use sum and specify the dimension along which to add the values?
Though, it's not clear to me how your data is structured/formatted. It will be better if you share the data files.
Mr Thadi
on 14 Dec 2023
Moved: Dyuman Joshi
on 14 Dec 2023
Voss
on 14 Dec 2023
Please share the code you have written so far to read the text file.
Answers (1)
Try this:
fileName = "radio.txt";
data = readmatrix(fileName, 'NumHeaderLines', 5)
% Turn nans into 0's.
data(isnan(data)) = 0;
% Sum up each column over all the rows in that column.
columnSums = sum(data, 1);
bar(columnSums);
grid on;
4 Comments
@Mr Thadi: Note that readmatrix() reads the sections of the file between the tables too, producing sections of data that I imagine you would rather ignore. Also, the data from the last row of each table ends up in the wrong columns. [Of course, if you want to assume that any row with a NaN should be skipped (i.e., that any valid row of a table in the file contains all non-NaN values), then that's easily done.]
fileName = "radio.txt";
L = readlines(fileName);
disp(L(105:147))
data = readmatrix(fileName, 'NumHeaderLines', 5);
disp(data(100:142,:))
Mr Thadi
on 14 Dec 2023
Image Analyst
on 14 Dec 2023
@Mr Thadi Why? Why do you want to mess with the complications of cell arrays, fopen, strcmp, etc. when you don't have to??? Why not do it like I said, or with the modification @Voss suggested about tossing out any rows with 1 or more nans in them? Voss's suggestion would be
fileName = "radio.txt";
data = readmatrix(fileName, 'NumHeaderLines', 5)
% Throw out any rows that have a nan in them.
badRows = any(isnan(data), 2);
data = data(~badRows, :);
% Sum up each column over all the rows in that column.
columnSums = sum(data, 1);
bar(columnSums);
grid on;
Categories
Find more on Spreadsheets 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!