Skip to content

Instantly share code, notes, and snippets.

@allsey87
Last active December 11, 2023 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allsey87/c749ee0d6bfa21b7dccb9b965ebc174d to your computer and use it in GitHub Desktop.
Save allsey87/c749ee0d6bfa21b7dccb9b965ebc174d to your computer and use it in GitHub Desktop.
How to plot data in Scilab from a CSV file (with first line as the header)
clear parse_my_data
clear path
clear inputs
clear index
function parse_my_data(f_data_path)
csv_data = read_csv(f_data_path);
num_data = strtod(csv_data(2:$,1:$));
num_data_points = size(num_data,1);
my_plot = figure();
my_plot.axes_size = [num_data_points*10 500];
my_plot.background = color("white");
my_plot.figure_name = fileparts(f_data_path, "fname");
my_axes = my_plot.children(1);
my_axes.margins = [0.05,0.05,0.05,0.05];
my_axes.tight_limits = "on";
my_axes.data_bounds = [0, -2.0; num_data_points, 2.0];
my_axes.axes_visible = ["on", "on", "off"];
plot(my_axes, [num_data(1:$,1:$)])
legend(my_axes, csv_data(1,1:$))
endfunction
path = '/home/allsey87/Workspace/pid-tuning/'
inputs = path + findfiles(path, 'pid*');
for index = 1:1:size(inputs,1)
parse_my_data(inputs(index));
end
@CaglayanDokme
Copy link

CaglayanDokme commented Mar 3, 2021

Hi,
Thanks for sharing your code. It helped me a lot. In my case, there was only one file including multiple columns.
To create a unique plot for each column, I wrote another script using your codes. You can find my script below.
Don't forget to modify the path :)

CSV Columns to Figures

path = "/home/.../someUglyFile.csv";

csvData = read_csv(path); // Fetch data from file
numData = strtod(csvData(2:$, 1:$)); // Exclude the title row
rowCount = size(numData, 1); // Get row count
columnCount = size(numData, 2); // Get column count

// Iterate for each column
for column = 1 : columnCount
myPlot = figure(); // Create a figure
myPlot.figure_name = csvData(1, column); // Determine figure name
plot(numData(:, column)); // Plot the related data
legend(csvData(1, column)); // Adjust graph legend
end

// xdel(winsid()) // Close all figure windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment