how to make the same length in the struct

i've a struct with filed of various length (see pics)
i want to make the same length of this array
and assign the zero in the data that is not there at the end
i try this ..but it's not correct
>> [Sis.d]
Error using horzcat
Dimensions of arrays being concatenated are not consistent.

댓글 수: 8

Matt J
Matt J 2023년 8월 28일
We cannot see the form of Sis from the .png file. It would be best to attach Sis itself in a .mat file.
Matt J
Matt J 2023년 8월 28일
The repost has the same issue. The Sis variable is not attached.
shamal
shamal 2023년 8월 29일
I replicated part of the structure by simplifying the data
shamal
shamal 2023년 8월 29일
이동: Stephen23 2023년 8월 29일
I want to use "Date" to sync the "dailyprof" values ​​in the matrix but in your example when you call padcat you don't use the "Date" field so it's not correct
a correct result:
Sistema Date dailyprof
XX1 25-feb-2023 4
26-feb-2023 5
27-feb-2023 6
XX2 26-feb-2023 7
27-feb-2023 8
28-feb-2023 9
Result
25-feb-2023 4 0
26-feb-2023 5 7
27-feb-2023 6 8
28-feb-2023 0 9
Stephen23
Stephen23 2023년 8월 29일
편집: Stephen23 2023년 8월 29일
@Luca Re: I moved your comment here and deleted my answer, because it is clear that you have not sufficiently explained what you are trying to achieve. At no point in your question do you mention anything like "sync" or synchronizing the data with dates or anything like that. Sorry, but my mind-reading crystal ball is currently being fixed as it broke last week. If you want further help, please explain what you want to achieve AND provide the expected output in a MATLAB syntax using the provided data.
Bruno Luong
Bruno Luong 2023년 8월 29일
편집: Bruno Luong 2023년 8월 29일
This thread becomes completely a mess. The problem asked now has nothing to do with Padding arrays in structure.
The other thread should not be closed.
shamal
shamal 2023년 8월 29일
편집: shamal 2023년 8월 29일
using last example i write code to do it:
%DATA "XX1"
data=[
'25-Feb-2008'
'26-Feb-2008'
'27-Feb-2008'
];
data=datetime(data);
%DAILYPROF "XX1"
dailyprof=[
4
5
6
];
b(1)=struct("Sistema",{"xx1"},"Date",data,"dailyprof",dailyprof);
%DATA "XX21"
data=[
'26-Feb-2008'
'27-Feb-2008'
'28-Feb-2008'
];
data=datetime(data);
%DAILYPROF "XX2"
dailyprof=[
7
8
9
];
b(2)=struct("Sistema",{"xx2"},"Date",data,"dailyprof",dailyprof);
%{
25-feb-2023 4 0
26-feb-2023 5 7
27-feb-2023 6 8
28-feb-2023 0 9
%}
v=[b.Date];
g=unique(v)
hh=zeros(size(g));
for i=1:numel(g)
for h=1:numel(b(1).Date)
if g(i)==b(1).Date(h)
hh(i)=b(1).dailyprof(h);
end
end
end
M(1)=struct("Sistema",{"xx1"},"Date",g,"dailyprof",hh);
hh=zeros(size(g));
for i=1:numel(g)
for h=1:numel(b(2).Date)
if g(i)==b(2).Date(h)
hh(i)=b(2).dailyprof(h);
end
end
end
M(2)=struct("Sistema",{"xx2"},"Date",g,"dailyprof",hh);
RESULT IS M!

댓글을 달려면 로그인하십시오.

 채택된 답변

Bruno Luong
Bruno Luong 2023년 8월 29일
load('matlab_sis.mat')
Date = unique(cat(1,Sis.Date));
DAILYPROOF = nan(length(Date),length(Sis));
for k=1:length(Sis)
sk = Sis(k);
[tf, i] = ismember(sk.Date, Date);
DAILYPROOF(i,k) = sk.dailyprof;
end
T = table(Date, DAILYPROOF)
T = 71×2 table
Date DAILYPROOF ___________ _______________________ 24-Feb-2008 1510 NaN NaN 25-Feb-2008 0 NaN NaN 26-Feb-2008 940 NaN NaN 27-Feb-2008 0 NaN NaN 28-Feb-2008 0 NaN NaN 29-Feb-2008 0 NaN NaN 01-Mar-2008 0 NaN NaN 02-Mar-2008 -70 NaN NaN 03-Mar-2008 -190 0 NaN 04-Mar-2008 -1360 0 NaN 05-Mar-2008 0 290 NaN 06-Mar-2008 0 1220 NaN 07-Mar-2008 0 0 NaN 08-Mar-2008 0 120 NaN 09-Mar-2008 180 0 NaN 10-Mar-2008 -140 0 NaN

댓글 수: 3

shamal
shamal 2023년 8월 29일
excellent..thanks a lot
Version without loop
n = length(Sis);
Date = {Sis.Date};
lgt = cellfun(@length, Date);
[Date, ~, i] = unique(cat(1,Date{:}));
j = repelem((1:n)',lgt(:));
DAILYPROOF = nan(length(Date),length(Sis));
DAILYPROOF(i+n*(j-1)) = cat(1,Sis.dailyprof);
T = table(Date, DAILYPROOF)
shamal
shamal 2023년 8월 30일
thank you

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bruno Luong
Bruno Luong 2023년 8월 28일
s=struct('a',rand(5,1),'b',rand(3,1),'c',rand(6,1))
s = struct with fields:
a: [5×1 double] b: [3×1 double] c: [6×1 double]
maxheight = max(structfun(@height,s));
padarrays = structfun(@(x) [x; zeros(maxheight-height(x),1)], s, 'unif', 0)
padarrays = struct with fields:
a: [6×1 double] b: [6×1 double] c: [6×1 double]
padarrays.a
ans = 6×1
0.8804 0.0268 0.8319 0.5125 0.7417 0
padarrays.b
ans = 6×1
0.5513 0.1335 0.7522 0 0 0
padarrays.c
ans = 6×1
0.4471 0.9683 0.7315 0.4339 0.3527 0.5825

댓글 수: 1

shamal
shamal 2023년 8월 28일
편집: shamal 2023년 8월 28일
i get this error4:
s=Sis.d;
Error using structfun
Inputs to STRUCTFUN must be scalar structures.
Error in untitled2 (line 6)
maxheight = max(structfun(@height,s));
because in the structure there are other camps besides the Sis.d

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2023년 8월 28일

댓글:

2023년 8월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by