Azzera filtri
Azzera filtri

Matlab Producing Different Values Running Entire Script VS Single Line Runs

4 visualizzazioni (ultimi 30 giorni)
As the title states, I am getting different values from Matlab's length function depending on whether I run the entire script (Using the "Run" button) or just line by line manually. I have no clue why this is occuring but the Matlab functions are not producing correct results themselves when this occurs. I have Matlab on two seperate machines and tried testing the code on both and found the same issue persists. I tried simplifying the code with a very trvial example with the same input structures and the error does not apply to the simpler case. It is just occuring within my main code. For context, I am generating a custom mesh in cylindrical coordinates. The issue occurs on lines 104-105:
stemp = [length(nodesPhis) length(nodesPhiMSim)];
maxNodesPhi = max(length(nodesPhis),length(nodesPhiMSim)); % Finds the max dimension required for the
% phi-component 3D matrix to be populated
If we run the script using the run button, we can see in the variable window that the vector for stemp is [40,53]:
But when I highlight the text to manually run the line, it says [60,53]:
For context, I only define stemp once and in that line 104. Nowhere else is it redefined. Not only this, but even more contradictions come up later in the code from these lines. The code uses the max element of that vector to produce a NAN matrix with one of the dimensions lengths being that max number. This matrix is called "meshPhi", it's size is (x,max(stemp),z) and it is defined as:
meshPhi = NaN(length(nodesR),maxNodesPhi,max(length(nodesZ1),length(nodesZ2))); % Create "empty" phi-mesh to populate
When I run the code with the button, it actually creates the correctly sized meshPhi matrix:
But as can be seen, the meshR and meshZ matrices which are supposed to be the same size, are using 53, and not 60. Those matrices are defined like this on lines 142 and 156:
meshZ = NaN(length(nodesR),maxNodesPhi,max(length(nodesZ1),length(nodesZ2))); % Create "empty" z-mesh to populate
meshR = NaN(length(nodesR),maxNodesPhi,max(length(nodesZ1),length(nodesZ2))); % Create "empty" r-mesh to populate
Exactly the same... so the only thing I can think of is it seems that Matlab is bugged in some way here. Nonetheless, I still wanted to post this question to see if possibly it's not a bug and instead I am potentially missing something or not using an internal function correctly.
Main Code: "TEST_DynCylMesh.m"
Supporting function: "sigdigits.m"
  15 Commenti
dpb
dpb il 19 Giu 2023
Modificato: dpb il 21 Giu 2023
@Bruno Luong -- that's the edge case, yes.
The doc actually reads as " For arrays with more dimensions, the length is max(size(X)). The length of an empty array is zero." I did leave off the for zero-length arrays part, yes, since they weren't pertinent in this particular thread.
So strictly in compliance, the form would be max(size((x))*~isempty(x)
x=zeros(1,0);
length(x)
ans = 0
max(size(x))*~isempty(x)
ans = 0
dpb
dpb il 19 Giu 2023
@John, while you've probably lost interest long ere now, with the update that height and width now operate on arrays as well, I'll change my advice to recommend using them instead.

Accedi per commentare.

Risposte (0)

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by