code to load file and plot columns
% Load the data from the file (assuming it's a text file with space/tab delimited
columns)
data = load('your_file.txt');
% Transpose the data if necessary
data = data'; % transpose if rows and columns are flipped
% Get the number of columns in the data
num_columns = size(data, 2);
% Plot each column separately
figure;
for i = 1:num_columns
subplot(num_columns, 1, i);
plot(data(:, i));
xlabel('Index');
ylabel(['Column ' num2str(i)]);
title(['Data in Column ' num2str(i)]);
end
-----------------------------------------------------------------------------------
---------------
code to read data from file and convert from signed to hex
% Read the signed decimal data from the file
data = load('your_file.txt'); % Assuming the file contains signed decimal data
% Convert signed decimal data to hexadecimal
hex_data = dec2hex(typecast(int16(data), 'uint16')); % Assuming 16-bit signed data
% Display the hexadecimal data
disp(hex_data);
-----------------------------------------------------------------
data types in matlab
In MATLAB, various data types are available to represent different kinds of
numerical and non-numerical data. Here are some common data types along with the
number of bytes they typically require:
1. **Numeric Data Types**:
- `double`: Double-precision floating-point. Requires 8 bytes (64 bits).
- `single`: Single-precision floating-point. Requires 4 bytes (32 bits).
- `int8`: 8-bit signed integer. Requires 1 byte (8 bits).
- `uint8`: 8-bit unsigned integer. Requires 1 byte (8 bits).
- `int16`: 16-bit signed integer. Requires 2 bytes (16 bits).
- `uint16`: 16-bit unsigned integer. Requires 2 bytes (16 bits).
- `int32`: 32-bit signed integer. Requires 4 bytes (32 bits).
- `uint32`: 32-bit unsigned integer. Requires 4 bytes (32 bits).
- `int64`: 64-bit signed integer. Requires 8 bytes (64 bits).
- `uint64`: 64-bit unsigned integer. Requires 8 bytes (64 bits).
2. **Logical Data Type**:
- `logical`: Logical (true/false). Typically stored as 1 byte.
3. **Character Data Type**:
- `char`: Character. Typically stored as 2 bytes per character in Unicode.
It's important to note that the actual memory usage might vary depending on factors
like the platform, MATLAB version, and memory alignment requirements.
Here's a quick summary of the data types and their memory usage in bytes:
- `double`: 8 bytes
- `single`: 4 bytes
- `int8`, `uint8`: 1 byte
- `int16`, `uint16`: 2 bytes
- `int32`, `uint32`: 4 bytes
- `int64`, `uint64`: 8 bytes
- `logical`: 1 byte
- `char`: Typically 2 bytes per character in Unicode.
Keep in mind that the memory usage might increase if you're working with arrays or
matrices of these data types.
-----------------------------------------------------------------------------------
---
access data row-wise
first_row = data(1,:);
-------------------------------------
another way to load file in matlab
data = importdata('your_file_name.txt');
This function automatically detects the format of the file and reads it
accordingly. It works with various formats like TXT, CSV, XLS, etc.
---------------------------------------
serial input and plot data column wise matlab
function sfun_serial_plot(block)
% Level-2 MATLAB file S-Function for plotting serial input
% Register number of input ports
block.NumInputPorts = 0;
% Register number of output ports
block.NumOutputPorts = 0;
% Register parameters
block.NumDialogPrms = 4;
block.DialogPrmsTunable = {'Nontunable', 'Nontunable', 'Nontunable', 'Nontunable'};
% Set the block simStateCompliance to default
block.SimStateCompliance = 'DefaultSimState';
% Register sample times
block.SampleTimes = [0.1 0]; % The first element is the sample time and the second
is the offset time
% Setup the initial conditions
block.SetPreCompOutPortInfoToDynamic;
% Define the initialization function
block.RegBlockMethod('InitializeConditions', @InitializeConditions);
% Define the outputs function
block.RegBlockMethod('Outputs', @Outputs);
% Define the termination function
block.RegBlockMethod('Terminate', @Terminate);
end
function InitializeConditions(block)
% Initialize the serial port and plot setup
% Retrieve serial port parameters
comPort = block.DialogPrm(1).Data;
baudRate = block.DialogPrm(2).Data;
dataBits = block.DialogPrm(3).Data;
stopBits = block.DialogPrm(4).Data;
% Open serial port
serialPort = serial(comPort);
set(serialPort, 'BaudRate', baudRate);
set(serialPort, 'DataBits', dataBits);
set(serialPort, 'StopBits', stopBits);
fopen(serialPort);
block.UserData.SerialPort = serialPort;
end
function Outputs(block)
% Read data from the serial port and plot column-wise
serialPort = block.UserData.SerialPort;
% Read data from serial port
data = fscanf(serialPort, '%f');
% Check if data is not empty
if ~isempty(data)
% Parse data into columns
numCols = block.DialogPrm(5).Data; % Number of columns to plot
numRows = numel(data) / numCols;
dataMatrix = reshape(data, numCols, numRows)';
% Plot data
figure;
plot(dataMatrix);
xlabel('Time');
ylabel('Data');
title('Serial Data Plot');
legend('Column 1', 'Column 2', 'Column 3'); % Add legend for each column
end
end
function Terminate(block)
% Close the serial port when simulation terminates
serialPort = block.UserData.SerialPort;
fclose(serialPort);
end
-----------------------------------
code to Convert ascii file to hex file
% Step 1: Read the ASCII file
filename_ascii = 'input_ascii_file.txt';
fid = fopen(filename_ascii, 'r');
if fid == -1
error('Could not open the file.');
end
ascii_data = fread(fid, '*char');
fclose(fid);
% Step 2: Convert ASCII data to hexadecimal representation
hex_data = dec2hex(ascii_data);
% Step 3: Write hexadecimal data to a new file
filename_hex = 'output_hex_file.hex';
fid = fopen(filename_hex, 'w');
if fid == -1
error('Could not create the file.');
end
fwrite(fid, hex_data');
fclose(fid);
disp('Conversion completed.');
-------------------------------------------------------------