-logfile option changes output

6 views (last 30 days)
Jason
Jason on 3 Feb 2025
Commented: Jason on 17 Feb 2025
I am working on an Azure Pipeline. I need to echo the script output to the pipeline, and check the output to see if the stage failed. My current PowerShell code looks like this.
$cur_dir = $Env:AGENT_BUILDDIRECTORY
$log_file = Join-Path $cur_dir '\log_file.txt'
$cmd = Join-Path $cur_dir '\base\modeling_tools\matlab\stupid_permissions_workaround.m'
Write-Host $cmd
$args = "-logfile $log_file -nodisplay -nosplash -r run('$cmd'); exit();"
Start-Process matlab -ArgumentList $args -Wait
Get-Content $log_file | Write-Host
if ($log_file -match 'TOTAL NUMBER OF FAILED BLOCKS:: 0'){
$rv = 0
}
The output saved in log_file.txt does not match the output when I run the script in Matlab. The output captured in log_file.txt is getting truncated, and I have no idea why. Is there a better way to capture the output than using the -logfile option?
Output when run inside MATLAB Output in log_file.txt
'TOTAL NUMBER OF FAILED BLOCKS:: 38' 'TOTAL NUMBER OF FAILED BLOCKS:: 38'
{'Check Name' } {'Time' } {'Check Name' } {'Time' }
{["STD-MBD-098 Top Level Model" ]} {[1.1502]} {["STD-MBD-098 Top Level Model" ]} {[2.1924]}
{'STD-MBD-122 Sample Time' } {[0.1825]} {'STD-MBD-122 Sample Time' } {[0.3057]}
{'STD-MBD-065 Relational Blocks' } {[2.0332]} {'STD-MBD-065 Relational Blocks' } {[3.6043]}
{'STD-MBD-163 Nested States Standard' } {[0.0032]} {'STD-MBD-163 Nested States Stand…'} {[0.0089]}
{'STD-MBD-108 States Entry: During: Exit:' } {[0.0198]} {'STD-MBD-108 States Entry: Durin…'} {[0.0413]}
{'STD-MBD-097 Foreground color' } {[0.1659]} {'STD-MBD-097 Foreground color' } {[0.3120]}
{'STD-MBD-092 Outport Color' } {[0.0805]} {'STD-MBD-092 Outport Color' } {[0.2048]}
{["STD-MBD-100 Name Display" ]} {[0.0272]} {["STD-MBD-100 Name Display" ]} {[0.0575]}
{'STD-MBD-164 MATLAB Functions Prohibited' } {[0.0162]} {'STD-MBD-164 MATLAB Functions Pr…'} {[0.0283]}
  1 Comment
Jason
Jason on 3 Feb 2025
I just tried capturing the output with diary( ), and truncation is there too.

Sign in to comment.

Accepted Answer

Aravind
Aravind on 12 Feb 2025
Hi @Jason,
From the output and code you shared, it seems you are storing the data in a cell array where each element is a string, and you are using the “disp” function to display the cell array directly. The truncation you are experiencing is due to how the “disp” function handles cell arrays, not because of the “-logfile” option. By default, “disp” only shows part of each cell to prevent long text from scrolling other cells off the Command Window's view. The display amount is determined by the command window's width.
When you run the script in MATLAB, you see the entire string because the command window is likely larger. However, when launched from a shell script, MATLAB opens with a smaller window due to the “-r” option, leading to text truncation. The “-logfile” option simply logs what is displayed in the command window.
Here are two workarounds:
  1. Index with Curly Braces: Access the content of the cell array using curly braces {} instead of parentheses (). This allows you to retrieve the string itself, which you can then display fully with the “disp” function. For more on cell array indexing, visit: https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html.
  2. Use “fprintf” Function: Instead of “disp,” use the “fprintf” function to print the entire string from the cell array. Access the string using curly braces and display it with “fprintf.” More information on “fprintf” can be found here: https://www.mathworks.com/help/matlab/ref/fprintf.html.
These workarounds should help you display the full content in the log file without truncation.
Hope this helps!
  1 Comment
Jason
Jason on 17 Feb 2025
That makes sense.
I'll just live with the truncated outputs for now. Rewriting old scripts in not in the current scope of work.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!