how to change disp for fprintf

1 view (last 30 days)
Emanuel Barrios
Emanuel Barrios on 28 Apr 2021
Edited: Adam Danz on 28 Apr 2021
%Algoritmo sist_ecs_lineales
n=input("Proporciona el número de incógnitas del sistema");
Au=input("Ingresa la matriz aumentada del sistema");
disp(Au)
[num_filas,num_columnas]=size(Au); %n Es el número de incognitas del sistema
disp("Primer paso de la Eliminación de Gauss (ESTRATEGIA DE PIVETEO NATURAL)")
disp("a)Elección del pivote")
pivote=Au(1,1)
disp("b) Poner ceros debajo de la columna de donde se eligio el pivote")
%Calcula los multiplicadores (escalares)
lambda1=Au(2,1)/pivote;
lambda2=Au(3,1)/pivote;
Au(2,:)=-lambda1*Au(1,:)+Au(2,:)
Au(3,:)=lambda2*Au(1,:)+Au(3,:)
disp("Primer paso de la Eliminación de Gauss (ESTRATEGIA DE PIVETEO NATURAL)")
disp("a)Elección del pivote")
pivote=Au(1,1)
disp("b) Poner ceros debajo de la columna de donde se eligio el pivote")
%Calcula los multiplicadores (escalares)
lambda1=Au(3,2)/pivote;
Au(3,:)=-lambda1*Au(2,:)+Au(3,:)
disp("SUSTITUCIÓN HACIA ATRAS")
%Fin algoritmo
  1 Comment
Adam Danz
Adam Danz on 28 Apr 2021
Edited: Adam Danz on 28 Apr 2021
We need to know what the expected inputs are to be displayed. Rather than needing to run your code, how about you provide some more specific examples of what is being displayed and how you would like it to appear using fprintf.

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 28 Apr 2021
Edited: Adam Danz on 28 Apr 2021
Starting in Matlab r2021a, you can use formattedDisplayText() to capture anything that is displayed by disp().
See a summary of this new feature in the Community Highlights.
If you want to use fprintf instead, there are several examples in the documentation but when it comes to displaying multidimensional arrays such as matrices, it gets tricky. See the example provided by Jan.
You could also use string or num2str for numeric conversion to strings or character arrays.
Examples
% Example 1
a = randi(9,3,3)
a = 3×3
9 3 5 1 2 1 8 5 9
formattedDisplayText(a)
ans =
" 9 3 5 1 2 1 8 5 9 "
string(a)
ans = 3×3 string array
"9" "3" "5" "1" "2" "1" "8" "5" "9"
num2str(a)
ans = 3×7 char array
'9 3 5' '1 2 1' '8 5 9'
% Example 2
b = {'a', 7; 'b', 12}
b = 2×2 cell array
{'a'} {[ 7]} {'b'} {[12]}
string(b)
ans = 2×2 string array
"a" "7" "b" "12"
formattedDisplayText(b)
ans =
" {'a'} {[ 7]} {'b'} {[12]} "

Categories

Find more on Card games in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!