Not enough trace back to find error

15 次查看(过去 30 天)
Gavin
Gavin 2024-10-15
评论: Umar 2024-10-25,2:31
Apparently I have {} where I should have () I guess. I get this warning but the traceback list doesn't go far enough to find my code line:
Warning: The following error was caught while executing 'onCleanup' class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
^^^^
Error in onCleanup/delete (line 25)
obj.task();
^^^^^^^^^^
Error in onCleanup/delete (line 25)
obj.task();
^^^^^^^^^^
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 60)
newCallback = @(source, event)executeCallback(ams, ...
^^^^^^^^^^^^^^^^^^^^^^^^
> In onCleanup/delete (line 25)
In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 60)
How do I go about finding it? dbstack doesn't work because the warning doesn't leave me in a place to use it.

回答(1 个)

Umar
Umar 2024-10-15

Hi @Gavin,

Glad to help you out again. After going through your comments and reviewing the documentation provided at the link below,

https://www.mathworks.com/help/matlab/ref/cell2mat.html

The error suggests that you are attempting to access an element of a variable using brace indexing { } but the variable does not support this operation. In MATLAB, brace indexing is used for accessing elements of cell arrays, while regular parentheses ( ) are used for accessing arrays or matrices.The function cell2mat expects a cell array where each cell contains data of the same type and dimension. If any cell contains a different type or an unsupported structure (like nested cells or objects), it will raise this warning. Here are steps listed below to identify the source of the error.

Check the Cell Array: Before calling cell2mat, inspect your cell array to ensure all cells contain compatible data types. You can do this with a loop:

     for i = 1:numel(C)
         disp(class(C{i}));
     end

Use Debugging Techniques: Since dbstack isn't providing useful context here, consider adding temporary breakpoints or using try-catch blocks around your code where cell2mat is called. For example:

     try
         A = cell2mat(C);
     catch ME
         disp(ME.message);
         % Optionally log or output more details about C
     end

Simplify Your Code: Isolate the segment of code that uses cell2mat. Create a minimal reproducible example where you suspect the error occurs and test it independently.

Example Analysis: Given your cell array example:

     C = {[1], [2 3 4];
          [5; 9], [6 7 8; 10 11 12]};

Running cell2mat(C) should work as all elements are numeric and can be concatenated. If you encounter issues, check if any modifications were made to C before this call.

If your data structure is complex or inconsistent, consider using alternative functions like vertcat or horzcat, which can handle concatenation without converting to a standard array.

By following these steps, you should be able to pinpoint the source of your warning and rectify any issues related to brace indexing and data compatibility in your MATLAB code.

Hope this helps.

Please let me know if you have any further questions.

  2 个评论
Gavin
Gavin 2024-10-24,19:47
My problem was that with a 3000 line program and no hint as to where the error was (traceback stopped before it got to my code as I originally inricated). Cell2mat doesn't exist in my code. The whole {} vs () vs [] thing is a pain. For a language with no firm typing and the way it can change your objects from one type to another it's a bit hard to track down errors. The command window helps if you break out into the debugger and then type object names to see what they have become.
Eventually I found the errant () and now it's working but that doesn't answer my original question of how to get a longer traceback that goes all the way into my own code, not stopping in the AD generated code.
Umar
Umar 2024-10-25,2:31

Hi @Gavin,

In MATLAB, the default behavior of the error traceback can indeed be limiting, especially in large programs. To enhance the traceback and include your own code, consider using the dbstop command. This command allows you to set breakpoints on errors, which can help you catch the error in your code directly.You can set a breakpoint for all errors by executing the following command in the Command Window:

dbstop if error

This will pause execution whenever an error occurs, allowing you to inspect the workspace and the call stack. You can then use the dbstack function to view the function call history, which can provide insights into where the error originated.

Hope this helps.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by