better way to do this in MATLAB?

dustynrobots

I'm creating a 3x3 plot of subplots, and I want to have some options for display. Each subplot shows the torque vs. time for one degree of freedom (ex. knee flexion/extension), but I'm trying to give options of whether to show right and left, torque normalized by subject's mass, avg or not, etc. Right now I'm explicitly coding these options, but is there a better way to give me a choice of say: left only, not normalized, show avg? Hmmm

plotRight = 1;
normalizeByMass = 0;
   figure(1);
    for DOF = 1:9
    subplot(3,3,DOF);  
    if normalizeByMass
        if plotRight
            plot(x, torqueRnorm(:,:,DOF), 'r');
            hold on
        end
        if plotLeft
            plot(x, torqueLnorm(:,:,DOF));
            hold on
        end
    else
        if plotRight
            plot(x, torqueR(:,:,DOF), 'r');
            hold on
        end
        if plotLeft
            plot(x, torqueL(:,:,DOF));
            hold on
        end
    end
end
plot(x, torqueRmean(:,DOF), 'k', 'LineWidth', 2);
hold on
plot(x, torqueLmean(:,DOF), 'k', 'LineWidth', 2);
hold on
ylabel('Hip');
title('X');
axis tight;  

and the same thing for the next subplot...

Thanks

Simon

Your approach is correct. It is much better to use variables and conditions as you did than to comment out lines manually each time you want to hide some plots, etc.

Now what you can do is wrapping everything in a function. And your parameters (plotLeft, plotRight…) would become the arguments of this function. Like this you hide the complexity and it frees your mind to build bigger things.

There are also little things you can do to improve the readability:

  1. Indent your code correctly. Matlab can help you: Ctrl-A Ctrl-I (or ⌘A ⌘I on mac) will fix the indentation in your whole file.

  2. hold on can be called just once after subplot

  3. use true and false for boolean values instead of 0 and 1

  4. you don't need a semicolon after figure, subplot, plot, xlabel, title, axis, and in general any instruction that returns nothing

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Is there a better way to do this? BASH

分類Dev

Is there a better way to do Insertion Sort?

分類Dev

Is there a better way to do this than echo?

分類Dev

Do you know a better way to write this method?

分類Dev

Is there a better way to do this code? Perhaps using filter or reduce?

分類Dev

Is there a better way of comparing dates

分類Dev

A better way to detect the cursor?

分類Dev

Better way to return boolean

分類Dev

A better way to introspect a capture

分類Dev

Is there a better way to loop code?

分類Dev

Is there a way to do matrix calculations with unknown variables in the matrix using Matlab?

分類Dev

Is there a better way to do modulo in a finite field when directly working on polynomials rather than binary numbers?

分類Dev

Is there a better way to define a global variable?

分類Dev

Decorator with Arguments: Would this be a better way?

分類Dev

Is this possible using LEAD or is there a better way?

分類Dev

Is there a way of making discord embeds better?

分類Dev

Better way to override a function definition

分類Dev

Is there a better way to divide this string into substrings?

分類Dev

Is there a better way to re-use plots Matplotlib?

分類Dev

Better way of capturing multiple same tags?

分類Dev

Better way to execute nested for loops to generate a dict?

分類Dev

Better way of error handling in Kafka Consumer

分類Dev

rxJava better way to wait on a specific sequence of values

分類Dev

Which way of setting fields value is better and why?

分類Dev

Which way of setting fields value is better and why?

分類Dev

AngularJS: Is there a better way to achieve this than the one specified?

分類Dev

Better way to write jquery add/remove class

分類Dev

better way to load 2 dropdown in mvc

分類Dev

Better way to check a list for specific elements - python

Related 関連記事

  1. 1

    Is there a better way to do this? BASH

  2. 2

    Is there a better way to do Insertion Sort?

  3. 3

    Is there a better way to do this than echo?

  4. 4

    Do you know a better way to write this method?

  5. 5

    Is there a better way to do this code? Perhaps using filter or reduce?

  6. 6

    Is there a better way of comparing dates

  7. 7

    A better way to detect the cursor?

  8. 8

    Better way to return boolean

  9. 9

    A better way to introspect a capture

  10. 10

    Is there a better way to loop code?

  11. 11

    Is there a way to do matrix calculations with unknown variables in the matrix using Matlab?

  12. 12

    Is there a better way to do modulo in a finite field when directly working on polynomials rather than binary numbers?

  13. 13

    Is there a better way to define a global variable?

  14. 14

    Decorator with Arguments: Would this be a better way?

  15. 15

    Is this possible using LEAD or is there a better way?

  16. 16

    Is there a way of making discord embeds better?

  17. 17

    Better way to override a function definition

  18. 18

    Is there a better way to divide this string into substrings?

  19. 19

    Is there a better way to re-use plots Matplotlib?

  20. 20

    Better way of capturing multiple same tags?

  21. 21

    Better way to execute nested for loops to generate a dict?

  22. 22

    Better way of error handling in Kafka Consumer

  23. 23

    rxJava better way to wait on a specific sequence of values

  24. 24

    Which way of setting fields value is better and why?

  25. 25

    Which way of setting fields value is better and why?

  26. 26

    AngularJS: Is there a better way to achieve this than the one specified?

  27. 27

    Better way to write jquery add/remove class

  28. 28

    better way to load 2 dropdown in mvc

  29. 29

    Better way to check a list for specific elements - python

ホットタグ

アーカイブ