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

3 Comments

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.
sure Mr.joshi .i am sharing data for few days in below text file .i am trying for cegrigade different variables with different columns for plot Temperature,Hight for different days in one plot.
Please share the code you have written so far to read the text file.

Sign in to comment.

Answers (1)

Try this:
fileName = "radio.txt";
data = readmatrix(fileName, 'NumHeaderLines', 5)
data = 4696×11
1.0e+04 * 0.1010 0.0016 0.0025 0.0024 0.0092 0.0019 0 0 0.0298 0.0353 0.0301 0.1007 0.0041 0.0026 0.0024 0.0086 0.0019 0.0308 0.0001 0.0299 0.0353 0.0302 0.1000 0.0100 0.0026 0.0024 0.0085 0.0019 0.0185 0.0002 0.0299 0.0354 0.0303 0.0995 0.0144 0.0026 0.0023 0.0085 0.0019 0.0185 0.0003 0.0300 0.0354 0.0303 0.0979 0.0288 0.0025 0.0023 0.0086 0.0018 0.0190 0.0006 0.0300 0.0353 0.0303 0.0978 0.0297 0.0025 0.0023 0.0086 0.0018 0.0190 0.0006 0.0300 0.0353 0.0303 0.0945 0.0598 0.0023 0.0021 0.0087 0.0017 0.0200 0.0009 0.0301 0.0351 0.0304 0.0925 0.0786 0.0022 0.0020 0.0087 0.0016 0.0230 0.0008 0.0302 0.0349 0.0305 0.0918 0.0852 0.0022 0.0019 0.0087 0.0016 0.0233 0.0008 0.0302 0.0349 0.0305 0.0913 0.0900 0.0022 0.0019 0.0085 0.0015 0.0235 0.0008 0.0302 0.0348 0.0305
% 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))
" 7.0 33265 -36.9 -67.5 3 0.62 90 81 975.1 983.5 975.5" " 6.4 33880 -35.5 -66.5 3 0.78 91 75 1006.3 1017.1 1006.8" " 5.2 35331 -33.5 -66.5 2 0.96 94 62 1076.8 1091.1 1077.5" " 5.0 95 59 " "Station information and sounding indices" " Station identifier: VOMM" " Station number: 43279" " Observation time: 191001/0000" " Station latitude: 13.00" " Station longitude: 80.18" " Station elevation: 16.0" " Showalter index: 0.78" " Lifted index: -3.29" " LIFT computed using virtual temperature: -4.06" " SWEAT index: 175.20" " K index: 33.70" " Cross totals index: 18.90" " Vertical totals index: 24.90" " Totals totals index: 43.80" " Convective Available Potential Energy: 1516.39" " CAPE using virtual temperature: 1699.37" " Convective Inhibition: -51.36" " CINS using virtual temperature: -26.28" " Equilibrum Level: 142.54" " Equilibrum Level using virtual temperature: 142.39" " Level of Free Convection: 797.78" " LFCT using virtual temperature: 827.95" " Bulk Richardson Number: 630.93" " Bulk Richardson Number using CAPV: 707.07" " Temp [K] of the Lifted Condensation Level: 295.26" "Pres [hPa] of the Lifted Condensation Level: 946.67" " Mean mixed layer potential temperature: 299.94" " Mean mixed layer mixing ratio: 18.08" " 1000 hPa to 500 hPa thickness: 5780.00" "Precipitable water [mm] for entire sounding: 52.32" "43279 VOMM Madras Observations at 12Z 01 Oct 2019" "-----------------------------------------------------------------------------" " PRES HGHT TEMP DWPT RELH MIXR DRCT SKNT THTA THTE THTV" " hPa m C C % g/kg deg knot K K K " "-----------------------------------------------------------------------------" " 1007.0 16 31.0 24.0 66 19.08 90 8 303.5 360.6 307.0" " 1000.0 80 29.2 23.2 70 18.28 115 8 302.4 356.7 305.6" " 993.0 142 28.8 23.0 71 18.17 140 9 302.6 356.6 305.8"
data = readmatrix(fileName, 'NumHeaderLines', 5);
disp(data(100:142,:))
1.0e+04 * 0.0007 3.3265 -0.0037 -0.0067 0.0003 0.0001 0.0090 0.0081 0.0975 0.0984 0.0975 0.0006 3.3880 -0.0036 -0.0066 0.0003 0.0001 0.0091 0.0075 0.1006 0.1017 0.1007 0.0005 3.5331 -0.0034 -0.0066 0.0002 0.0001 0.0094 0.0062 0.1077 0.1091 0.1077 0.0005 0.0095 0.0059 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 4.3279 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0013 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0080 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0016 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0001 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN -0.0003 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN -0.0004 NaN NaN NaN NaN NaN NaN NaN 0.0175 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0034 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0019 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0025 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0044 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.1516 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.1699 NaN NaN NaN NaN NaN NaN NaN NaN -0.0051 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN -0.0026 NaN NaN NaN NaN NaN NaN NaN NaN 0.0143 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0142 NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0798 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0828 NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0631 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0707 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0295 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0947 NaN NaN NaN NaN NaN NaN NaN NaN 0.0300 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0018 NaN NaN NaN NaN NaN 0.1000 NaN NaN 0.0500 NaN NaN 0.5780 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.0052 NaN NaN NaN NaN 4.3279 NaN NaN NaN NaN NaN 0.0001 NaN 0.2019 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.1007 0.0016 0.0031 0.0024 0.0066 0.0019 0.0090 0.0008 0.0303 0.0361 0.0307 0.1000 0.0080 0.0029 0.0023 0.0070 0.0018 0.0115 0.0008 0.0302 0.0357 0.0306 0.0993 0.0142 0.0029 0.0023 0.0071 0.0018 0.0140 0.0009 0.0303 0.0357 0.0306
mr voss thanku for your response but please understand i need to convert data into cell by using following commands only fopen,fscanf,fgets,strcmp,str2num.
@Mr Thadi: Please share the code you are currently using to read the file.
@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;

Sign in to comment.

Products

Release

R2016b

Asked:

on 12 Dec 2023

Commented:

on 14 Dec 2023

Community Treasure Hunt

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

Start Hunting!