create a code to read a text file from desktop & plot column a vs column b in matlab

To read a text file from the desktop and plot two columns in MATLAB, you can follow these steps:

  1. Specify the file path: Replace 'C:\Users\Username\Desktop\filename.txt' with the actual path of your text file on the desktop.

  2. Read the file using the importdata function: This function reads data from the specified file and returns a structure containing the data.

  3. Extract the desired columns from the imported data: Access the columns required for the plot. Assuming you want to plot column A against column B, retrieve them using dot notation.

  4. Plot the data: Use the plot function to create a line plot of the extracted columns.

Here's an example code snippet that accomplishes this:

main.m
% Specify the file path
filepath = 'C:\Users\Username\Desktop\filename.txt';

% Read the file
data = importdata(filepath);

% Extract the desired columns
columnA = data.data(:, 1); % replace ':, 1' with the actual column index
columnB = data.data(:, 2); % replace ':, 2' with the actual column index

% Plot column A against column B
plot(columnA, columnB);

% Add labels and title
xlabel('Column A');
ylabel('Column B');
title('Plot of Column A vs Column B');

% Optionally, customize the plot appearance
grid on;
% Add legend, etc.

% Save the plot as an image, if required
% saveas(gcf, 'plot.png');
603 chars
25 lines

Make sure to replace 'C:\Users\Username\Desktop\filename.txt' with the actual path and filename of your text file, and adjust the column indices in the columnA and columnB lines to match the desired columns in your file.

You can customize the plot appearance, add legends, gridlines, etc., according to your requirements.

Remember to save the plot using the saveas function if you want to save it as an image.

I hope this helps!

related categories

gistlibby LogSnag