Home > Software design >  Matlab plot to ignore underscores?
Matlab plot to ignore underscores?

Time:10-11

I have lots of file names that are labelled in the form:

test1_cvariable1_cvariable2_rvariable.csv

To load and process them all automatically I'm using the code:

%filename
txt = ['test1_cvariable1_cvariable2_rvariable.csv']

%step 1
RNG = [23 0 1539 8];
M = csvread(txt,23,0,RNG);

I'd like to then plot a graph with the corresponding title, but owing to how matlab interprets underscores "_" if I use:

    %plot time vs variable
plot(M(:,1),M(:,8))
xlabel('Time (s)')
ylabel('Var')
title(['Var ' txt])

Gives a title in the form

test1cvariable1cvariable2rvariable

with the c,c & r as subscripts (apologies idk how to do this here, ironically)

How do I get Matlab to not make the character immediately after the underscore as a subscript?

CodePudding user response:

You can set the Interpreter property to ‘none’ to disable MATLAB’s usual default which is ‘TeX’. This works for most plotting text functions like title, xlabel, or ylabel.

For example:

title('savvy_iguana', 'Interpreter', 'none')

CodePudding user response:

You have to replace _ with \_ (see this mathworks answer). You can do it automatically using strrep:

title(['Var ' strrep(txt,'_','\_')]
  • Related