Resize cell array to match size of second cell array

Hi i have two cell arrays like the following where im using a camera for measuring temperature. Wire, Body and Lense are dynamically added, depending if the camera operatior descides to measure theses spots on the cam or not.
The problem is, as in this examle, im getting a error: "Dimeonsions of arrays are being concatenated or not consistend.". I can see why, tho im not really sure how to fix this, and so far a simple application restart did the job just right, tho thats not a nice fix.
If seen something with cellfun/arrayfun but im not sure on how to do it.
TL;DR
the size of these two arrays can change at any time, and when this happens, i need the smaller one to have the same size as the bigger one, filling the new added values to match the new size with something like 0 or "UPDATED".
newColumn =
1×6 cell array
{'Date'} {'Time'} {'IP Address'} {'Wire'} {'Body'} {'Lense'}
newData =
1×5 string array
"1970-01-01←" "02:36:17.304←" "10.x.x.x←" "35" "43"
Im error occurs here:
% oldData = a 21x5 array
oldData = [app.UITable.Data]
newData = rowData
updatedData = [newData;oldData];
% Verkleinert die Größe des Arrays damit sich das Programm
% nicht aufhängt oder zu langsam wird.
maxEntries = app.AnzahlmaxEintrgeinAppListeEditField.Value;
if size(updatedData, 1) > maxEntries
updatedData = updatedData(1:maxEntries, 1:size(updatedData, 2));
end
app.UITable.Data = updatedData;

10 Comments

Which array is concerned? Where do which error message occur? Please post a copy of the complete message.
If i remember correctly the error happens here and this error message i posted was the complete message
updatedData = [newData;oldData];
I even managed to come up with some code, tho it was a mess and despite it working, it was bad. Basically what i did is checking the size of the two, and took the smaller one and added 0's to it
Example:
Array1's size is 1x5.
Array2's size is 1x10
So i looped through Array1 from 5 to 10 in a for-loop and was adding values like that:
Array1(1, i) = "Example";
The problem is likely due to the values being dynamically added, per your description. This relinquishes control of where the values are added within the array. If the order of the values within the two arrays matter, then you must enter those values using indexing rather than entering them dynamically.
Example:
people = ["John","Abby","Ralf","Jane"];
scores = [100, 80, 20];
There is a missing score but we have no idea whose score is missing.
Assuming the "people" variable in my example is the independent variable, meaning that you control that, then the "scores" array is the dependent variable and should be preallocated to the same size as the independent variable. Then, indexing should be used to assign values to scores depending on whose data the scores belong to.
people = ["John","Abby","Ralf","Jane"];
scores = nan(size(people))
scores = 1×4
NaN NaN NaN NaN
% Enter score for "Jane"
scores(strcmp(people, "Jane")) = 20
scores = 1×4
NaN NaN NaN 20
% Enter scores for the rest of the people,
% except the person with missing data
scores(strcmp(people, "Abby")) = 80
scores = 1×4
NaN 80 NaN 20
scores(strcmp(people, "John")) = 100
scores = 1×4
100 80 NaN 20
Now all of the scores as in the same order as people and we see that Ralf is missing a score.
I did something like this but I had the same issue like your code example would give me probably.
The good thing was it did work, the problem was if the bigger array was getting smaller, it did change the array sizes correctly, but never removed the "unused" cells, meaning they were still displayed because of that in the tabcontrol
Did you figure out how to remove the unused cells? Sounds like you solved this :)
Well sadly not as the column stays and the data is just empty or "<missing>" i think. but other than that it kinda worked
Are the empty or missing values causing a problem or is the issue that they just don't look nice?
No it still works. Its just like if i'd try adding " " to the cell. I guess i forgot to also remove the column. Im going to rewrite the code that i had for that and post it here.
Sadly i already ditched the old one so i would've shared it.
Edit
I recreated the code and it was similar to this one.
arr1 = {"1", "2", "3", "4", "5"};
arr2 = {"1", "2", "3", "4", "5", "6", "7", "8","9", "10"};
if size(arr1, 2) < size(arr2, 2)
% array 1 is smaller
for i=size(arr1, 2):size(arr2, 2)
arr1(1, i) = { "hi" };
end
elseif size(arr1, 2) > size(arr2, 2)
% array 1 is bigger
arr1(size(arr2, 2)+1:size(arr1, 2)) = [];
end
arr1
arr2
If the array was bigger:
>> test
arr1 =
1×10 cell array
{["1"]} {["2"]} {["3"]} {["4"]} {["hi"]} {["hi"]} {["hi"]} {["hi"]} {["hi"]} {["hi"]}
arr2 =
1×10 cell array
{["1"]} {["2"]} {["3"]} {["4"]} {["5"]} {["6"]} {["7"]} {["8"]} {["9"]} {["10"]}
If the array was smaller:
>> test
arr1 =
1×5 cell array
{["1"]} {["2"]} {["3"]} {["4"]} {["5"]}
arr2 =
1×5 cell array
{["1"]} {["2"]} {["3"]} {["4"]} {["5"]}
I just need to integrate this into the tab control now
@Marcel, this can be acheived with 1 line of code by using padarray.
Yeah pretty sure and i was trying to find a solution like this :/

Sign in to comment.

 Accepted Answer

Okay so i came up with a working solution after intense debugging and frustration so big depression would be a joke.
Basically, what im doing with this function is feeding it the row data (arrayIn), which changes size on the fly, like 1x2, 1x3. 1x6, and everytime the row data changes, i also receive the same amount of data for the columns, so i have to have the column data the same size as my row data.
This is my row data
tempContent = searchFile(app, picturePath, "TTR=");
tempContent = strrep(tempContent, "TTR=", ""); % returns a value like "22,24,45" (temps in °C)
properTempContent = regexp(tempContent,',','split');
temperatureData = ...
strrep(str2double(extract(properTempContent, digitsPattern)), '\n', '');
rowData = "";
rowData = [flip(temperatureData), rowData];
And this is my header/column data
% Im looking for this line and try to get the values in between the
% brackets ()
% EPR=raar:PI(Header1),raar:MI(Header2),raar:env_5a65ba8a(Header4),asen:AS,user:UC
params = string(searchFile(app, picturePath, "EPR="));
params_split = regexp(params,',','split');
params_split = strrep(params_split, "EPR=", "");
% Get Headlines
headerData = "";
for i=1:length(params_split)
if contains(params_split(i), "raar:")
exp = '[^()]*';
matchStr = regexp(params_split(i),exp,'match');
% Konvertiere HTML Sonderzeichen/Encoding zu normale
% Buchstaben
data = strrep(matchStr{1,2}, "%C3%B6", "ö");
headerData = [data, headerData];
end
end
So this means if the tempContent (in row data) was "11,22,33", the params (in header data) would be something like "Header1,Header2,Header3", since these values always kinda come in a pair. So if it would be "11, 22,", the headers would be "Header1,Header".
The problem now was on how to append that to the table control. and luckily it works now.
function [inputArray, outputArray] = fixArraySize(app, arrayIn, arrayOut, fillData)
if length(arrayIn) == 0
arrayIn = arrayOut;
end
if size(arrayOut, 2) < size(arrayIn, 2)
% array 1 is smaller
for i=size(arrayOut, 2):size(arrayIn, 2)
arrayOut(1, i) = { fillData };
end
elseif size(arrayOut, 2) > size(arrayIn, 2)
% array 1 is bigger
arrayOut(size(arrayIn, 2)+1:size(arrayOut, 2)) = [];
end
inputArray = arrayIn;
outputArray = arrayOut;
end
How i use this function:
[oldArray, newArray] = fixArraySize(app, rowData, newData, "Test");
Data before any change of the columns
rowData =
1×8 string array
Columns 1 through 3
"18.11.2022" "08:32:02" "10.70.121.47←"
Columns 4 through 8
"M16D-Thermal-TR←" "Unknown" "23" "23" "23"
newData =
1×8 string array
Columns 1 through 3
"18.11.2022" "08:32:02" "10.70.121.47←"
Columns 4 through 8
"M16D-Thermal-TR←" "Unknown" "23" "23" "23"
oldArray =
1×8 string array
Columns 1 through 3
"18.11.2022" "08:32:02" "10.70.121.47←"
Columns 4 through 8
"M16D-Thermal-TR←" "Unknown" "23" "23" "23"
newArray =
1×8 string array
Columns 1 through 3
"18.11.2022" "08:32:02" "10.70.121.47←"
Columns 4 through 8
"M16D-Thermal-TR←" "Unknown" "23" "23" "23"
Data after the change
rowData =
1×7 string array
Columns 1 through 3
"18.11.2022" "08:33:19" "10.70.121.47←"
Columns 4 through 7
"M16D-Thermal-TR←" "Unknown" "23" "23"
newData =
1×7 string array
Columns 1 through 3
"18.11.2022" "08:33:19" "10.70.121.47←"
Columns 4 through 7
"M16D-Thermal-TR←" "Unknown" "23" "23"
oldArray =
1×7 string array
Columns 1 through 3
"18.11.2022" "08:33:19" "10.70.121.47←"
Columns 4 through 7
"M16D-Thermal-TR←" "Unknown" "23" "23"
newArray =
1×7 string array
Columns 1 through 3
"18.11.2022" "08:33:19" "10.70.121.47←"
Columns 4 through 7
"M16D-Thermal-TR←" "Unknown" "23" "23"
How i displayed the data
rowData
newData
[oldArray, newArray] = fixArraySize(app, rowData, newData, "Test");
oldArray
newArray
Not sure if this is useful, but the code is very complex in my opinion.

1 Comment

A simplification:
function [In, Out] = fixArraySize(In, Out, fillData)
% I've removed "app", which is not used here.
nIn = size(In, 2);
nOut = size(Out, 2);
if isempty(In)
In = Out;
elseif nOut < nIn % array 1 is smaller
% No loop needed. Should it be nOut+1:nIn ?
Out(1, nOut:nIn) = {fillData};
elseif nOut > nIn % array 1 is bigger
Out(nIn+1:nOut) = [];
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 16 Nov 2022

Commented:

Jan
on 19 Nov 2022

Community Treasure Hunt

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

Start Hunting!