Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAT_T_STRING not correctly recognized? #263

Open
BruhGreg opened this issue Dec 10, 2024 · 3 comments
Open

MAT_T_STRING not correctly recognized? #263

BruhGreg opened this issue Dec 10, 2024 · 3 comments

Comments

@BruhGreg
Copy link

BruhGreg commented Dec 10, 2024

I wrote a little snippet to parse .mat file:

void _printProperties(matvar_t *mat_var);

int main()
{
	Log::Init();

	std::string filepath;
	std::cout << "Please enter the path to your .mat file: ";
	std::getline(std::cin, filepath);

	mat_t *mat = Mat_Open(filepath.c_str(), MAT_ACC_RDONLY);
	if (mat == nullptr) {
		LOG_ERROR("Failed to open .mat file: {0}", filepath);
		return 1;
	}

	matvar_t *matVar = nullptr;
	while ((matVar = Mat_VarReadNext(mat)) != nullptr) {
		_printProperties(matVar);

		if (matVar->data) {
			unsigned size = matVar->nbytes / matVar->data_size;
			if (matVar->data_type == MAT_T_DOUBLE) {
				const double *data = static_cast<const double *>(matVar->data);

				for (size_t i = 0; i < matVar->dims[0]; ++i) {
					for (size_t j = 0; j < matVar->dims[1]; ++j) {
						LOG_INFO("\t[{0}][{1}] = {2}", i, j, data[i + j * matVar->dims[0]]);
					}
				}
			}
			else if (matVar->data_type == MAT_T_UTF8) {
				const char *data = static_cast<const char *>(matVar->data);
				LOG_INFO("Char: {0}", std::string(data, matVar->nbytes));
			}
			else if (matVar->data_type == MAT_T_STRING) {
				const std::string *data = static_cast<const std::string *>(matVar->data);
				LOG_INFO("String: {0}", data->c_str());
			}
		}
		else {
			LOG_INFO("Data: (null)");
		}
		LOG_INFO("");

		Mat_VarFree(matVar);
	}

	Mat_Close(mat);

	return 0;
}

std::string _getType(matio_types type)
{
	switch (type) {
		case MAT_T_UNKNOWN:
			return "Unknown";
		case MAT_T_INT8:
			return "Int8";
		case MAT_T_UINT8:
			return "UInt8";
		case MAT_T_INT16:
			return "Int16";
		case MAT_T_UINT16:
			return "UInt16";
		case MAT_T_INT32:
			return "Int32";
		case MAT_T_UINT32:
			return "UInt32";
		case MAT_T_SINGLE:
			return "Single";
		case MAT_T_DOUBLE:
			return "Double";
		case MAT_T_INT64:
			return "Int64";
		case MAT_T_UINT64:
			return "UInt64";
		case MAT_T_MATRIX:
			return "Matrix";
		case MAT_T_COMPRESSED:
			return "Compressed";
		case MAT_T_UTF8:
			return "UTF8";
		case MAT_T_UTF16:
			return "UTF16";
		case MAT_T_UTF32:
			return "UTF32";
		case MAT_T_STRING:
			return "String";
		case MAT_T_CELL:
			return "Cell";
		case MAT_T_STRUCT:
			return "Struct";
		case MAT_T_ARRAY:
			return "Array";
		case MAT_T_FUNCTION:
			return "Function";
		default:
			return "Unknown Default";
	}
}

void _printProperties(matvar_t *mat_var)
{
	std::string msg;
	std::string var_name = mat_var->name ? mat_var->name : "(unnamed)";

	msg += fmt::format("---\nVariable: {0}, Type: {1}, Rank: {2}\n",
		var_name,
		_getType(mat_var->data_type),
		mat_var->rank);

	msg += fmt::format("Dimension: ");
	for (int i = 0; i < mat_var->rank; ++i) {
		msg += fmt::format("{0} x ", mat_var->dims[i]);
	}
	msg = msg.substr(0, msg.length() - 3); // Remove last " x "
	msg += "\n";
	LOG_INFO(msg);
}

When i feed in my .mat file
Screenshot 2024-12-10 135733

Note: the last variable str_last is actually char typed, it is named so to be placed at the last of the list, and used as an indicatior to show the list has come to the end.

The output is as follows:

Please enter the path to your .mat file: c:\Users\xxx\Desktop\test.mat
---
Variable: a, Type: Double, Rank: 2
Dimension: 3 x 3

        [0][0] = 1
        [0][1] = 2.2
        [0][2] = 3
        [1][0] = 4
        [1][1] = 5
        [1][2] = 6
        [2][0] = 7
        [2][1] = 0
        [2][2] = 0

---
Variable: b, Type: Int8, Rank: 2
Dimension: 1 x 1


---
Variable: c, Type: Double, Rank: 2
Dimension: 1 x 1

        [0][0] = 2

---
Variable: cell, Type: Cell, Rank: 2
Dimension: 2 x 3


---
Variable: char, Type: UTF8, Rank: 2
Dimension: 1 x 10

Char: char_value

---
Variable: ee, Type: Double, Rank: 2
Dimension: 1 x 3

        [0][0] = 1
        [0][1] = 2
        [0][2] = 3

---
Variable: (unnamed), Type: Unknown, Rank: 0
Dimensio

Data: (null)

---
Variable: (unnamed), Type: Unknown, Rank: 0
Dimensio

Data: (null)

---
Variable: str_last, Type: UTF8, Rank: 2
Dimension: 1 x 3

Char: end

---
Variable: , Type: UInt8, Rank: 2
Dimension: 1 x 1304

As is shown, string data in .mat file are not recognized, as in the type is treated as MAT_T_UNKNOWN, and the weird thing is, if there's a string in the .mat file, there will be an extra Variable typed UInt8

@BruhGreg BruhGreg changed the title MAT_T_STRING not correctly recognized? MAT_T_STRING not correctly recognized? Dec 10, 2024
@tbeu
Copy link
Owner

tbeu commented Dec 10, 2024

Can you please provide the MAT file?

@BruhGreg
Copy link
Author

yes, the mat file is attached.
test_mat.zip

@tbeu tbeu added the duplicate label Dec 10, 2024
@tbeu
Copy link
Owner

tbeu commented Dec 10, 2024

Duplicate of #98.

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

No branches or pull requests

2 participants