MATLAB Answers

Too many output arguments when using "rowfun" function

7 views (last 30 days)
GS76
GS76 on 11 Jan 2020
Commented: Stephen Cobeldick on 14 Jan 2020
I am trying to run through through table to generate a number of dat files for running in a FEA software.
I have created a function (very cumbersome, I am sure as I am a newbie programmer) to generate the data files. The function runs successfully one by one, but I was hoping to use the "rowfun" function to generate as many dat files as I need.
All associated files are attached.
The function below is called with:
% Apply the function, |GS_aij|, to the rows of the table, |CC|.
rowfun(GS_aij,CC,"NumOutputs",0)
I then receive the error message. "Too many output arguments".
What am I doing wrong? Any assistance or guidance would be appreciated.
The called function is as follows:
function GS_aij(fa,D,GL,dh,T,OR,ES,RL_ES,NP,P,HDI,SDI,...
HD1,HD2,HD3,HD4,HD5,HD6,HD7,HD8,HD9,HD10,HD11,HD12,HD13,...
HD14,HD15,HD16,HD17,HD18,HD19,HD20,ET,E,v,BASE,RO,CT)
% GS_aij The generation of *.dat files for Ansys Mechanical APDL.
% The generation of *.dat files for Ansys Mechanical APDL. The most
% critical variables have been selected as part of a Design of
% Experiments, to optimise the aij calibration coefficients.
filemat=load('aij_bij.mat');
aij_new=filemat.aij;
Updating the "aij" calibration matrix with the new inputs.
aij_new(3)="/filname,"+fa+",0!Changes the jobname for the analysis.";
aij_new(5)="*set,HD_D,"+D+"!Sets the variable for the (strain gauge mean diameter) for the gauge, in microns.";
aij_new(6)="*set,HD_GL,"+GL+"!Sets the variable for the (strain gauge grid length) for the gauge, in microns.";
aij_new(7)="*set,HD_D0,"+dh+"!Sets the variable for the drilled hole diameter, in microns.";
aij_new(8)="*set,sample_th,"+T+"!Sets the variable for the (work piece thickness) for the gauge, in microns.";
aij_new(9)="*set,sample_radia,"+OR+"!Sets the variable for the (work piece outer radius) for the gauge, in microns.";
aij_new(10)="*set,es,"+ES+"!Sets the variable for the finite element size (microns). Recommended size should not be larger than smallest hole depth increment.";
aij_new(11)="*set,rl_es,"+RL_ES+"!Sets the variable for the element size. Maximum element size should be equal to the smallest hole depth increment.";
aij_new(12)="*set,sample_stress,"+P+"!Sets the variable for the pressure, on the inner radius of drilled hole.";
aij_new(13)="*set,n,"+HDI+"!Sets the variable for the number of drilled hole depth increments.";
aij_new(14)="*set,p,"+SDI+"!Sets the variable for the number of stress depth increments.";
aij_new(15)="*set,np,"+NP+"!Sets the variable for the number of nodal planes for general axisymmetric analysis.";
% Drilled depth increments, from the surface in microns.
switch HDI
case '1'
aij_new(16)="*set,HD,"+HD1+"!Sets the variable for the number of 1st drilled hole depth increment.";
case '2'
aij_new(16)="*set,HD,"+HD2+"!Sets the variable for the number of 2nd drilled hole depth increment.";
case '3'
aij_new(16)="*set,HD,"+HD3+"!Sets the variable for the number of 3rd drilled hole depth increment.";
case '4'
aij_new(16)="*set,HD,"+HD4+"!Sets the variable for the number of 4th drilled hole depth increment.";
case '5'
aij_new(16)="*set,HD,"+HD5+"!Sets the variable for the number of 5th drilled hole depth increment.";
case '6'
aij_new(16)="*set,HD,"+HD6+"!Sets the variable for the number of 6th drilled hole depth increment.";
case '7'
aij_new(16)="*set,HD,"+HD7+"!Sets the variable for the number of 7th drilled hole depth increment.";
case '8'
aij_new(16)="*set,HD,"+HD8+"!Sets the variable for the number of 8th drilled hole depth increment.";
case '9'
aij_new(16)="*set,HD,"+HD9+"!Sets the variable for the number of 9th drilled hole depth increment.";
case '10'
aij_new(16)="*set,HD,"+HD10+"!Sets the variable for the number of 10th drilled hole depth increment.";
case '11'
aij_new(16)="*set,HD,"+HD11+"!Sets the variable for the number of 11th drilled hole depth increment.";
case '12'
aij_new(16)="*set,HD,"+HD12+"!Sets the variable for the number of 12th drilled hole depth increment.";
case '13'
aij_new(16)="*set,HD,"+HD13+"!Sets the variable for the number of 13th drilled hole depth increment.";
case '14'
aij_new(16)="*set,HD,"+HD14+"!Sets the variable for the number of 14th drilled hole depth increment.";
case '15'
aij_new(16)="*set,HD,"+HD15+"!Sets the variable for the number of 15th drilled hole depth increment.";
case '16'
aij_new(16)="*set,HD,"+HD16+"!Sets the variable for the number of 16th drilled hole depth increment.";
case '17'
aij_new(16)="*set,HD,"+HD17+"!Sets the variable for the number of 17th drilled hole depth increment.";
case '18'
aij_new(16)="*set,HD,"+HD18+"!Sets the variable for the number of 18th drilled hole depth increment.";
case '19'
aij_new(16)="*set,HD,"+HD19+"!Sets the variable for the number of 19th drilled hole depth increment.";
case '20'
aij_new(16)="*set,HD,"+HD20+"!Sets the variable for the number of 20th drilled hole depth increment.";
end
aij_new(18)="et,1,"+ET+",,np!Definition of the element type (linear solid272 or the quadratic solid273) and the number of nodal planes.";
aij_new(20)="mp,ex,1,"+E+"!Enter the modulus of elasticity, in MPa.";
aij_new(21)="mp,prxy,1,"+v+"!Enter the poisson's ratio.";
% Outer Radius
switch RO
case 'RO_NA'
aij_new(65)="!d,all,,,,,,,,,,,";
case 'RO_UX'
aij_new(65)="d,all,,,,,,ux,,,,,";
case 'RO_UY'
aij_new(65)="d,all,,,,,,,uy,,,,";
case 'RO_UXY'
aij_new(65)="d,all,,,,,,ux,uy,,,,";
end
% Base
switch BASE
case 'Base_UYZ'
aij_new(68)="d,all,,,,,,,uy,uz,,,";
case 'Base_UXYZ'
aij_new(68)="d,all,,,,,,ux,uy,uz,,,";
end
aij_new(74)="eqslv,jcg,"+CT;
% Stress depth increments in microns.
switch SDI
case '1'
aij_new(77)="nsel,r,loc,z,1.5,-"+HD1+"-1.5";
case '2'
aij_new(77)="nsel,r,loc,z,-"+HD1+"+2.5,-"+HD2+"-2.5";
case '3'
aij_new(77)="nsel,r,loc,z,-"+HD2+"+2.5,-"+HD3+"-2.5";
case '4'
aij_new(77)="nsel,r,loc,z,-"+HD3+"+2.5,-"+HD4+"-2.5";
case '5'
aij_new(77)="nsel,r,loc,z,-"+HD4+"+2.5,-"+HD5+"-2.5";
case '6'
aij_new(77)="nsel,r,loc,z,-"+HD5+"+2.5,-"+HD6+"-2.5";
case '7'
aij_new(77)="nsel,r,loc,z,-"+HD6+"+2.5,-"+HD7+"-2.5";
case '8'
aij_new(77)="nsel,r,loc,z,-"+HD7+"+2.5,-"+HD8+"-2.5";
case '9'
aij_new(77)="nsel,r,loc,z,-"+HD8+"+2.5,-"+HD9+"-2.5";
case '10'
aij_new(77)="nsel,r,loc,z,-"+HD9+"+2.5,-"+HD10+"-2.5";
case '11'
aij_new(77)="nsel,r,loc,z,-"+HD10+"+2.5,-"+HD11+"-2.5";
case '12'
aij_new(77)="nsel,r,loc,z,-"+HD11+"+2.5,-"+HD12+"-2.5";
case '13'
aij_new(77)="nsel,r,loc,z,-"+HD12+"+2.5,-"+HD13+"-2.5";
case '14'
aij_new(77)="nsel,r,loc,z,-"+HD13+"+2.5,-"+HD14+"-2.5";
case '15'
aij_new(77)="nsel,r,loc,z,-"+HD14+"+2.5,-"+HD15+"-2.5";
case '16'
aij_new(77)="nsel,r,loc,z,-"+HD15+"+2.5,-"+HD16+"-2.5";
case '17'
aij_new(77)="nsel,r,loc,z,-"+HD16+"+2.5,-"+HD17+"-2.5";
case '18'
aij_new(77)="nsel,r,loc,z,-"+HD17+"+2.5,-"+HD18+"-2.5";
case '19'
aij_new(77)="nsel,r,loc,z,-"+HD18+"+2.5,-"+HD19+"-2.5";
case '20'
aij_new(77)="nsel,r,loc,z,-"+HD19+"+2.5,-"+HD20+"-2.5";
end
aij_new(99)="*cfopen,"+fa+"_n_%n%_p_%p%_es_%es%_rl_es_%rl_es%_np_%np%_sr_%sample_radia%_st_%sample_th%,txt";
% Creation of the *.dat file.
filenamedat=fa+".dat";
fileID=fopen(filenamedat,'w'); % Open and name an empty text
% file.
fprintf(fileID,'%s\n',aij_new); % Write data to the text file.
fclose(fileID); % Close the text file.
clear % Clear the workspace.
clc % Clear command window.
end

  15 Comments

Guillaume
Guillaume on 13 Jan 2020
Numbered variables are always a bad idea. How many lines of code, each a potential source of bug do you have just to cope with these numbered HDx variables? What if tomorrow, you want to change the formatting of the text (eg. add a space after the commas)? You then have to edit all these lines of code. Using indexing as suggested by David, all this can be collapsed into just one line.
However, with regards to your immediate problem, it's fairly clear from the error message. Somewhere you have a call to a function Creation. Indeed, towards the end of your code you have the lines:
aij_new(99)="*cfopen,"+fa+"_n_%n%_p_%p%_es_%es%_rl_es_%rl_es%_np_%np%_sr_%sample_radia%_st_%sample_th%,txt";
Creation of the *.dat file.
filenamedat=fa+".dat";
The Creation line is probably meant to be a comment.
GS76
GS76 on 13 Jan 2020
My bad! Once I corrected a statement that used to be commented out, then everything was working.
Thank you to everyone for the help.
GS76
GS76 on 13 Jan 2020
Thank you Guillaume.
I really appreciate the feedback and insight.
And thank you for the answer below. Thank you for the suggested alternative design. Beautifully streamlined and efficient.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 13 Jan 2020
Glad the problem is resolved, however I would strongly recommend to refactor the code. I like rowfun, varfun, etc. but for you I think it's the wrong function to use and it greatly complicates your code. Passing that amount of variables to a function, particularly numbered variables is a problem.
Instead, I would suggest a completely different design. Most of your code follows the same pattern, build a string consisting of 3 parts: two literal parts and a part in between taken from a table variable. Said variable can be static or numbered. The whole lot is added to a given index of a variable. Rather than implementing all that description in code, abstract it into a cell array or table:
%mods: a table describing the modifications to be done. Consists of 5 variables:
% index: which index of aij to write to
% prefix: literal string prefix
% suffix: literal string suffix
% varname: name of variable containing text to insert between prefix and suffix. May need a number appended
% dynsource: if not empty, add value of variable dynsource to varname
mods = cell2table(...
{3, "/filname,", "fa", ",0!Changes the jobname for the analysis.", [];
5, "*set,HD_D,", "D", "!Sets the variable for the (strain gauge mean diameter) for the gauge, in microns.", [];
6, "*set,HD_GL,", "GL", "!Sets the variable for the (strain gauge grid length) for the gauge, in microns.", [];
7, "*set,HD_D0,", "dh", "!Sets the variable for the drilled hole diameter, in microns.", []; ...some lines omitted
16, "*set,HD,", "HD", "!Sets the variable for the number of 1st drilled hole depth increment.", "HDI";
18, "et,1,", "ET", ",,np!Definition of the element type (linear solid272 or the quadratic solid273) and the number of nodal planes.", []}, ...more lines omitted
'VariableNames', {'index', 'prefix', 'suffix', 'varname', 'dynsource'});
I'm assuming the variable names that you've defined in your function are the same as the actual variable names of your table.
I would then use a plain loop instead of rowfun:
filemat=load('aij_bij.mat');
for CCrow = 1:height(CC)
aij = filemat.aij
%now loop over the rows of mods
for mrow = 1:height(mods)
sourcevar = mods.varname(mrow);
if ~isempty(mods.dynsource(mrow))
sourcevar = sprintf('%s%d', sourcevar, CC.{CCrow, mods.dynsource(row)});
end
aij(mods.index(mrow)) = mods.prefix(mrow) + CC.{CCrow, sourcevar} + mods.suffix(mrow);
end
end
%...
That's already most of your code shortened to a few lines. It's also easier to edit if you want to modify the text format, etc. The mods table could even be read from a text or excel file.
You can either have a separate table for the SDI values or add two optional variables to the mods table.

More Answers (0)

Categories

Products


Release

R2019b

Translated by