How to export to workspace a 4D array?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示

Basically the first function is generating values between a certain range. This values are indexes in order to have access to certain locations of the 4D array.
When writting mode is active, I save the value of 0.365 to the current location. At the end I would like to export the 4D array to workspace with all the stored values.
The 4D array has the following dimensions: [50 311 91 61] = 86318050 single values
However the array seems to be not able to be exported due to the size. I thought that changing it into single values will help but it seems to be too big.
Any ideas on how to fix this?
採用された回答
Ameer Hamza
2020 年 5 月 17 日
Instead of using "To workspace" block, try using assignin(): https://www.mathworks.com/help/releases/R2020a/matlab/ref/assignin.html to directly save the values in the base workspace. For example, write the following line in your function
assignin('base', 'base_variable_name', array_filled)
12 件のコメント
Mariana
2020 年 5 月 17 日
function [[out,array_filled] ] = fcn(new, input, status, initial_array, export)
%Multidimensional array dimensions
xmin = 0; xmax = 200; xincr = 5;
ymin = -25; ymax = 25; yincr = 5;
zmin = 0; zmax = 180; zincr = 5;
hmin = -10; hmax = 10; hincr = 5;
xvec = xmin : xincr : xmax;
yvec = ymin : yincr : ymax;
zvec = zmin : zincr : zmax;
hvec = hmin : hincr : hmax;
x2xidx = @(xval) round((xval - xmin)/xincr) + 1;
y2yidx = @(yval) round((yval - ymin)/yincr) + 1;
z2zidx = @(zval) round((zval - zmin)/zincr) + 1;
h2hidx = @(hval) round((hval - hmin)/hincr) + 1;
persistent storage
persistent a_new
if isempty(storage)
storage = initial_array;
a_new = 0;
end
%(read only)
if status == 1
a_new = storage(index1,index2,index3,index4);
end
%write only)
if status == 0
storage(index1,index2,index3,index4);
a_new = 50;
end
if export == 1
multi_array = storage;
else
multi_array = single(zeros(nx,ny,nz,nh));
end
out = a_new;
array_filled = multi_array;
Mariana
2020 年 5 月 17 日
I should change it like this?
function [[out,array_filled] ] = fcn(new, input, status, initial_array, export)
assignin('base', 'multi_array', array_filled)
%Multidimensional array dimensions
xmin = 0; xmax = 200; xincr = 5;
ymin = -25; ymax = 25; yincr = 5;
zmin = 0; zmax = 180; zincr = 5;
hmin = -10; hmax = 10; hincr = 5;
xvec = xmin : xincr : xmax;
yvec = ymin : yincr : ymax;
zvec = zmin : zincr : zmax;
hvec = hmin : hincr : hmax;
x2xidx = @(xval) round((xval - xmin)/xincr) + 1;
y2yidx = @(yval) round((yval - ymin)/yincr) + 1;
z2zidx = @(zval) round((zval - zmin)/zincr) + 1;
h2hidx = @(hval) round((hval - hmin)/hincr) + 1;
persistent storage
persistent a_new
if isempty(storage)
storage = initial_array;
a_new = 0;
end
%(read only)
if status == 1
a_new = storage(index1,index2,index3,index4);
end
%write only)
if status == 0
storage(index1,index2,index3,index4);
a_new = 50;
end
if export == 1
multi_array = storage;
else
multi_array = single(zeros(nx,ny,nz,nh));
end
out = a_new;
Ameer Hamza
2020 年 5 月 17 日
No, add this line at the end of your function definition like this
function [[out,array_filled] ] = fcn(new, input, status, initial_array, export)
%Multidimensional array dimensions
xmin = 0; xmax = 200; xincr = 5;
ymin = -25; ymax = 25; yincr = 5;
zmin = 0; zmax = 180; zincr = 5;
hmin = -10; hmax = 10; hincr = 5;
xvec = xmin : xincr : xmax;
yvec = ymin : yincr : ymax;
zvec = zmin : zincr : zmax;
hvec = hmin : hincr : hmax;
x2xidx = @(xval) round((xval - xmin)/xincr) + 1;
y2yidx = @(yval) round((yval - ymin)/yincr) + 1;
z2zidx = @(zval) round((zval - zmin)/zincr) + 1;
h2hidx = @(hval) round((hval - hmin)/hincr) + 1;
persistent storage
persistent a_new
if isempty(storage)
storage = initial_array;
a_new = 0;
end
%(read only)
if status == 1
a_new = storage(index1,index2,index3,index4);
end
%write only)
if status == 0
storage(index1,index2,index3,index4);
a_new = 50;
end
if export == 1
multi_array = storage;
else
multi_array = single(zeros(nx,ny,nz,nh));
end
out = a_new;
assignin('base', 'multi_array', array_filled)
Mariana
2020 年 5 月 17 日
But I need to remove the "to workspace" block?
Ameer Hamza
2020 年 5 月 17 日
Yes, you should remove it. assignin will already save values in the base workspace.
Mariana
2020 年 5 月 17 日
Function 'assignin' not supported for code generation.
Function 'MATLAB Function' (#132.4706.4751), line 154, column 1:
"assignin('base', 'array_filled', multi_array)"
Launch diagnostic report.
Ameer Hamza
2020 年 5 月 17 日
Oh! I forgot it does not support code generation. Change the function definition to this
function [[out,array_filled] ] = fcn(new, input, status, initial_array, export)
coder.extrinsic('assignin') % <===== added this line
%Multidimensional array dimensions
xmin = 0; xmax = 200; xincr = 5;
ymin = -25; ymax = 25; yincr = 5;
zmin = 0; zmax = 180; zincr = 5;
hmin = -10; hmax = 10; hincr = 5;
xvec = xmin : xincr : xmax;
yvec = ymin : yincr : ymax;
zvec = zmin : zincr : zmax;
hvec = hmin : hincr : hmax;
x2xidx = @(xval) round((xval - xmin)/xincr) + 1;
y2yidx = @(yval) round((yval - ymin)/yincr) + 1;
z2zidx = @(zval) round((zval - zmin)/zincr) + 1;
h2hidx = @(hval) round((hval - hmin)/hincr) + 1;
persistent storage
persistent a_new
if isempty(storage)
storage = initial_array;
a_new = 0;
end
%(read only)
if status == 1
a_new = storage(index1,index2,index3,index4);
end
%write only)
if status == 0
storage(index1,index2,index3,index4);
a_new = 50;
end
if export == 1
multi_array = storage;
else
multi_array = single(zeros(nx,ny,nz,nh));
end
out = a_new;
assignin('base', 'multi_array', array_filled)
Mariana
2020 年 5 月 17 日
You were right. It works now.
Thanks a lot!
Mariana
2020 年 5 月 17 日
Is it possible to load a variable in simulink workspace of 400MB?
Ameer Hamza
2020 年 5 月 17 日
I am not sure. Maybe you can try the constant block to load the value in Simulink workspace. Otherwise, you can again use evalin(), opposite of assignin, to directly read the value from the base workspace. Another approach (preferable) is mentioned in Rubem's answer here: https://www.mathworks.com/matlabcentral/answers/110060-simulink-matlab-function-block#answer_385817
Mariana
2020 年 6 月 3 日
I have added the variable of 400MB into the simulink workspace, but I am not able to compile the model. I have an error message that there is no heap space, any suugestion on how to solve this issue?
Ameer Hamza
2020 年 6 月 4 日
I haven't worked with Simulink compiler, so I don't know about this issue. Maybe the following will be helpful
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Signal Operations についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
