Error using cat: Dimensions of matrices being concatenated are not consistent.

2 views (last 30 days)
Hello!
I have this program that, at the end, I need to assemble a matrix with 5 columns: var1, var2, var3, var4 and hh. However I get this error: Error using cat Dimensions of matrices being concatenated are not consistent.
clear all;
close all;
str='/home/pink/Documents';
folder_name = uigetdir(str);
files = dir(fullfile(folder_name,'*.dat'));
curr_folder=pwd;
cd(folder_name);
for i = 1:length(files);
fileID = fopen(files(i).name);
ref = fread(fileID,[500 500], 'float32');
teste = find(ref<0);
ref(teste) = NaN;
rot = rot90(ref);
latitude = [-3.63965:-0.00898:-8.12601];
longitude = [-37.5098:0.00906:-32.9831];
L1 = 1;
L2 = 500;
for LL = L1:L2;
array_latitude (1:500,LL) = latitude;
end
array_lat = array_latitude(1:500,1:500);
array_lat;
for Lll = L1:L2;
array_longitude(Lll,1:500) = longitude;
end
array_long = array_longitude(1:500,1:500);
array_long;
var1 = find(rot>=20);
var2 = array_lat(var1);
var3 = array_long(var1);
var4 = rot(var1);
for j = 1:length(var1);
name = files(i).name;
s = strsplit(name,'_');
km = s{3};
alt{j,1} = km;
end
hh = cellfun(@str2double,alt);
B = cat(2,var1,var2,var3,var4,hh);
end
The problem is in this loop. For each file that opens, I have a different size for var1 and, consequently, the size of hh should change and is not changing, so it says that the dimensions are different.
for j = 1:length(var1);
name = files(i).name;
s = strsplit(name,'_');
km = s{3};
alt{j,1} = km;
end
hh = cellfun(@str2double,alt);
I would like help to solve this problem.

Answers (1)

dpb
dpb on 3 Sep 2020
latitude = [-3.63965:-0.00898:-8.12601];
longitude = [-37.5098:0.00906:-32.9831];
str='/home/pink/Documents';
folder_name = uigetdir(str);
files = dir(fullfile(folder_name,'*.dat'));
curr_folder=pwd;
cd(folder_name);
for i = 1:length(files);
fileID = fopen(files(i).name);
ref = fread(fileID,[500 500], 'float32');
ref((ref<0)=NaN;
rot = rot90(ref);
array_lat=repmat(latitude(500,1);
array_long=repmat(longitude,500,1);
ix20 = find(rot>=20);
lat20 = array_lat(ix20);
long20 = array_long(ix20);
rot20 = rot(ix20);
hh=zeros(ix20,1);
for j = 1:length(ix20);
name = files(i).name;
s = strsplit(name,'_');
hh(j)=str2double(s{3});
end
B=cat(2,ix20,lat20,long20,rot20,hh);
end
  2 Comments
Walter Roberson
Walter Roberson on 4 Sep 2020
Right, the code should not be rotating the indices returned by find() and should not be treating the indices as a length. But that logic was copied from the original logic, so you will need to be the one to debug it.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices 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!