Can someone help me with this matlab code

In summary, the conversation discusses building a code to draw a diamond of user-specified size using the fprintf command. The code has a bug where extra asterisks appear on the corners for even numbers. The solution is to use elseif statements to prevent multiple if statements from being executed. It is also recommended to learn how to use debugging features and breakpoints in MATLAB.
  • #1
Dell
590
0
i need to build a code which asks the user for the size of the desired diamond and then draws it, i can get this to workl perfectly by using the fprintf command and building the diamond out of 4 parts,: see code-

Code:
clc
clear
h=input('enter height of diamond :');
y=1;
while y<=h/2
    for x=1:h-1
        if x<=((h)/2-(y-1))
            fprintf(1,'*\t');
        end
        if x>(h)/2-(y-1) & x<(h)/2+(y-1)
            fprintf(1,'\t');
        end
        if x>=((h)/2+(y-1)) &x<=h-1
            fprintf(1,'*\t');
        end
    end
    y=y+1;
    fprintf(1,'\n')
end

while y>h/2 &y<=h
    for x=1:h-1
        if x<=((h)/2-(h-y))
            fprintf(1,'*\t');
        end
        if x>(h)/2-(h-y) & x<(h)/2+(h-y)
            fprintf(1,'\t');
        end
        if x>=((h)/2+(h-y)) &x<=h-1
            fprintf(1,'*\t');
        end
    end
    y=y+1;
    fprintf(1,'\n')
end

but for any even numbers i put in as my size, i get 2 extra asterixs on the corners, can anyone see how i chan get rid of them?
 
Physics news on Phys.org
  • #2
Look inside the loops that create the horizontal rows of asterisks. You actually have a degenerate case where two of the if statements get executed! (At the very top and very bottom rows)
 
  • #3
thats what i thought must be happening, but i cannot find where, could you show me
 
  • #4
I think I mentioned this in a different thread to you, but it's absolutely essential to learn how to use the debugging features of MATLAB, and especially breakpoints:
http://ieee-uffc.org/ultrasonics/software/MATLAB/Lecture8/Lecture8_2.htm
http://en.wikibooks.org/wiki/MATLAB_Programming/Debugging_M_Files

By stepping through, you'll find that the two cases are happening right at the middle (h/2) of the line. Use this information to learn how to use breakpoints.
 
  • #5
i think that the problem comes fro the 1st line, where (y-1)=0 therefore, (h)/2-(y-1) is equal to x>(h)/2+(y-1) and so both the 1st and 3rd if commands are executed,
so i tried making one <= and one > but that messed up my whole diamond
 
  • #6
Use some elseif statements to prevent the execution of more than one of the if statements:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/brqy1c1-1.html#brqy1c1-3

You could use a break statement, but this is considered very poor programming form.
 
Last edited by a moderator:
  • #7
thanks, works pefrectly
 

1. What is Matlab and why do I need help with it?

Matlab is a programming language and software platform commonly used in scientific and engineering fields. It is used for data analysis, visualization, and mathematical computations. Many individuals may need help with Matlab because it is a complex language with a steep learning curve.

2. How do I find someone to help me with my Matlab code?

There are several options for finding someone to help with your Matlab code. You can reach out to colleagues or classmates who are familiar with Matlab, join online forums and communities, or hire a professional Matlab programmer.

3. Can someone help me with debugging my Matlab code?

Yes, there are many individuals who specialize in debugging Matlab code and can assist you with identifying and fixing any errors in your code. It is helpful to provide them with a clear explanation of the issue and any relevant code snippets.

4. How long does it typically take to get help with a Matlab code?

The time it takes to get help with a Matlab code can vary depending on the complexity of the code and the availability of the person helping you. It is best to reach out to someone as soon as you encounter an issue to give them enough time to assist you.

5. Is it possible to learn Matlab on my own, or do I need someone to help me?

It is possible to learn Matlab on your own through online tutorials, textbooks, and practice. However, having someone with experience and knowledge of the language can greatly accelerate your learning process and help you troubleshoot any difficulties you may encounter.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
821
  • Engineering and Comp Sci Homework Help
Replies
3
Views
805
  • Engineering and Comp Sci Homework Help
Replies
6
Views
851
  • Engineering and Comp Sci Homework Help
Replies
1
Views
875
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
952
  • Engineering and Comp Sci Homework Help
Replies
1
Views
935
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
885
  • Engineering and Comp Sci Homework Help
Replies
1
Views
941
Back
Top