Resize cell array to match size of second cell array

37 views (last 30 days)
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
Marcel
Marcel on 18 Nov 2022
Yeah pretty sure and i was trying to find a solution like this :/

Sign in to comment.

Accepted Answer

Marcel
Marcel on 18 Nov 2022
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
Jan
Jan on 19 Nov 2022
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 Data Type Identification in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!