I have multiple txt files which I wanted a matlab script to read. In this example I have two text file info1.txt and info2.txt.

Info1.txt looks like below:

Time_s F1_Hz F2_Hz F3_Hz F4_Hz

5.380881 168.044737 1829.872713 3131.559116 3213.668349

5.387131 161.213890 2095.135172 2711.543722 3234.763980

5.393381 157.556636 2249.510191 2328.096433 3224.830663

5.399631 152.934386 1837.870876 3006.768940 3269.394789

5.405881 143.134450 1870.416293 3197.394115 3316.147912

5.412131 145.898056 2258.175966 3218.393904 3532.813035

5.418381 150.491464 2269.422128 3213.139318 4225.212839

5.424631 148.670403 2311.284231 3182.551708 4223.545307

5.430881 153.939863 2379.293644 3054.841641 3136.397634

5.437131 149.255460 2439.435819 3031.251115 3082.328426

5.443381 152.705234 2329.376344 3064.071602 3502.160620

5.449631 156.758662 2282.807907 3060.245108 3327.679379

5.455881 160.635833 2306.747237 3010.713170 4220.430522

5.462131 165.759322 1833.151734 2982.880950 3758.899103

5.468381 174.216231 2041.281640 2991.233836 4136.807691

5.474631 178.761006 2391.839203 2918.998599 4053.885226

5.480881 201.155537 1628.226332 2935.538464 4108.894904

5.487131 225.481050 1623.869490 2863.388348 4105.493022

5.493381 253.502413 1579.837856 2786.406148 4057.924438

5.499631 259.084692 1590.070449 2772.983177 4039.225299

5.505881 277.796770 1585.384071 2792.721624 4013.537284

5.512131 284.205955 1483.018675 2787.610500 3994.652451

Info2.txt is also similar.

The following is my Matlab script;

*********************************************

clear all;

%initialize

fid = zeros(2);

a = cell(2,5);

seg_a = 'C:\Users\user\Desktop\PhD\Substudy\Substudy5\data\info';

seg_b = '.txt';

filename = strcat(seg_a, mat2str(1), seg_b);

fid(1) = fopen(filename);

a(1) = textscan(fid(1), '%s %s %s %s %s');

filename = strcat(seg_a, mat2str(2), seg_b);

fid(2) = fopen(filename);

a(2) = textscan(fid(2), '%s %s %s %s %s');

*********************************************

When running the above script, I get the following error

EDU>> ujian2reverse

In an assignment A(:) = B, the number of elements in A and B

must be the same.

Error in ujian2reverse (line 15)

a(1) = textscan(fid(1), '%s %s %s %s %s');

---------------------------------------------------------

When my script was like this:

***************************************************

clear all;

%initialize

fid = zeros(2);

seg_a = 'C:\Users\user\Desktop\PhD\Substudy\Substudy5\data\info';

seg_b = '.txt';

filename = strcat(seg_a, mat2str(1), seg_b);

fid(1) = fopen(filename);

a = textscan(fid(1), '%s %s %s %s %s');

filename = strcat(seg_a, mat2str(2), seg_b);

fid(2) = fopen(filename);

b = textscan(fid(2), '%s %s %s %s %s');

******************************************************

there was no error

a returned each with

b returned each with

SO MY ERROR IS AT INITIALIZING 'a'. How do I correctly initialize memory allocation for 'a' cell array. I have tried all sorts of combinations but it is still giving an error.

More Abu Dzarr's questions See All
Similar questions and discussions